Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy007.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.suspendPolicy;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.suspendPolicy()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks that for a MethodExitEvent set, <BR>45* the method returns values corresponding to one <BR>46* which suspends the most threads. <BR>47* The cases to check include event sets containing <BR>48* one to three threads with all possible combinations: <BR>49* NONE, THREAD, ALL, NONE+THREAD, NONE+ALL, <BR>50* THREAD+ALL, NONE+THREAD+ALL <BR>51* <BR>52* The test has three phases and works as follows. <BR>53* <BR>54* In first phase, <BR>55* upon launching debuggee's VM which will be suspended, <BR>56* a debugger waits for the VMStartEvent within a predefined <BR>57* time interval. If no the VMStartEvent received, the test is FAILED. <BR>58* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>59* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>60* and waits for the event within the predefined time interval. <BR>61* If no the ClassPrepareEvent received, the test is FAILED. <BR>62* Upon getting the ClassPrepareEvent, <BR>63* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>64* within debuggee's special methodForCommunication(). <BR>65* <BR>66* In second phase to check the above, <BR>67* the debugger and the debuggee perform the following loop. <BR>68* - The debugger sets up a MethodExitRequest, resumes <BR>69* the debuggee, and waits for the MethodExitEvent. <BR>70* - The debuggee invokes the special method to be resulting in the event.<BR>71* - Upon getting new event, the debugger <BR>72* performs the check corresponding to the event. <BR>73* <BR>74* Note. To inform each other of needed actions, the debugger and <BR>75* and the debuggee use debuggee's variable "instruction". <BR>76* In third phase, at the end, <BR>77* the debuggee changes the value of the "instruction" <BR>78* to inform the debugger of checks finished, and both end. <BR>79* <BR>80*/8182public class suspendpolicy007 extends JDIBase {8384public static void main (String argv[]) {8586int result = run(argv, System.out);8788System.exit(result + PASS_BASE);89}9091public static int run (String argv[], PrintStream out) {9293int exitCode = new suspendpolicy007().runThis(argv, out);9495if (exitCode != PASSED) {96System.out.println("TEST FAILED");97}98return testExitCode;99}100101// ************************************************ test parameters102103private String debuggeeName =104"nsk.jdi.EventSet.suspendPolicy.suspendpolicy007a";105106private String testedClassName =107"nsk.jdi.EventSet.suspendPolicy.suspendpolicy007aTestClass";108109//====================================================== test program110111private int runThis (String argv[], PrintStream out) {112113argsHandler = new ArgumentHandler(argv);114logHandler = new Log(out, argsHandler);115Binder binder = new Binder(argsHandler, logHandler);116117waitTime = argsHandler.getWaitTime() * 60000;118119try {120log2("launching a debuggee :");121log2(" " + debuggeeName);122if (argsHandler.verbose()) {123debuggee = binder.bindToDebugee(debuggeeName + " -vbs");124} else {125debuggee = binder.bindToDebugee(debuggeeName);126}127if (debuggee == null) {128log3("ERROR: no debuggee launched");129return FAILED;130}131log2("debuggee launched");132} catch ( Exception e ) {133log3("ERROR: Exception : " + e);134log2(" test cancelled");135return FAILED;136}137138debuggee.redirectOutput(logHandler);139140vm = debuggee.VM();141142eventQueue = vm.eventQueue();143if (eventQueue == null) {144log3("ERROR: eventQueue == null : TEST ABORTED");145vm.exit(PASS_BASE);146return FAILED;147}148149log2("invocation of the method runTest()");150switch (runTest()) {151152case 0 : log2("test phase has finished normally");153log2(" waiting for the debuggee to finish ...");154debuggee.waitFor();155156log2("......getting the debuggee's exit status");157int status = debuggee.getStatus();158if (status != PASS_BASE) {159log3("ERROR: debuggee returned UNEXPECTED exit status: " +160status + " != PASS_BASE");161testExitCode = FAILED;162} else {163log2("......debuggee returned expected exit status: " +164status + " == PASS_BASE");165}166break;167168default : log3("ERROR: runTest() returned unexpected value");169170case 1 : log3("test phase has not finished normally: debuggee is still alive");171log2("......forcing: vm.exit();");172testExitCode = FAILED;173try {174vm.exit(PASS_BASE);175} catch ( Exception e ) {176log3("ERROR: Exception : e");177}178break;179180case 2 : log3("test cancelled due to VMDisconnectedException");181log2("......trying: vm.process().destroy();");182testExitCode = FAILED;183try {184Process vmProcess = vm.process();185if (vmProcess != null) {186vmProcess.destroy();187}188} catch ( Exception e ) {189log3("ERROR: Exception : e");190}191break;192}193194return testExitCode;195}196197198/*199* Return value: 0 - normal end of the test200* 1 - ubnormal end of the test201* 2 - VMDisconnectedException while test phase202*/203204private int runTest() {205206try {207testRun();208209log2("waiting for VMDeathEvent");210getEventSet();211if ( !(eventIterator.nextEvent() instanceof VMDeathEvent) ) {212log3("ERROR: last event is not the VMDeathEvent");213return 1;214}215216log2("waiting for VMDisconnectEvent");217getEventSet();218if ( !(eventIterator.nextEvent() instanceof VMDisconnectEvent) ) {219log3("ERROR: last event is not the VMDisconnectEvent");220return 1;221}222223return 0;224225} catch ( VMDisconnectedException e ) {226log3("ERROR: VMDisconnectedException : " + e);227return 2;228} catch ( Exception e ) {229log3("ERROR: Exception : " + e);230return 1;231}232233}234235private void testRun()236throws JDITestRuntimeException, Exception {237238eventRManager = vm.eventRequestManager();239240log2("......getting ClassPrepareEvent for debuggee's class");241ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();242cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);243cpRequest.addClassFilter(debuggeeName);244cpRequest.enable();245vm.resume();246getEventSet();247cpRequest.disable();248249ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();250debuggeeClass = event.referenceType();251252if (!debuggeeClass.name().equals(debuggeeName))253throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");254log2(" received: ClassPrepareEvent for debuggeeClass");255256log2("......setting up ClassPrepareEvent for breakpointForCommunication");257258String bPointMethod = "methodForCommunication";259String lineForComm = "lineForComm";260BreakpointRequest bpRequest;261ThreadReference mainThread = debuggee.threadByNameOrThrow("main");262bpRequest = settingBreakpoint(mainThread,263debuggeeClass,264bPointMethod, lineForComm, "zero");265bpRequest.enable();266267vm.resume();268269//------------------------------------------------------ testing section270271log1(" TESTING BEGINS");272273EventRequest eventRequest1 = null;274EventRequest eventRequest2 = null;275EventRequest eventRequest3 = null;276277final int SUSPEND_POLICY = EventRequest.SUSPEND_NONE;278final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;279final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;280final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;281282int policyExpected[] = { SUSPEND_NONE,283SUSPEND_THREAD,284SUSPEND_ALL,285SUSPEND_THREAD,286SUSPEND_ALL,287SUSPEND_ALL,288SUSPEND_ALL };289int policy = 0;290291ReferenceType testClassReference = null;292293String fieldName = "var1";294295296for (int i = 0; ; i++) {297298299breakpointForCommunication();300301int instruction = ((IntegerValue)302(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();303if (instruction == 0) {304vm.resume();305break;306}307308309log1(":::::: case: # " + i);310311//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part312313switch (i) {314315case 0:316testClassReference =317(ReferenceType) vm.classesByName(testedClassName).get(0);318319eventRequest1 = settingMethodExitRequest (mainThread,320testClassReference,321SUSPEND_NONE, "MethodExitRequest1");322eventRequest1.enable();323break;324325case 1:326eventRequest2 = settingMethodExitRequest (mainThread,327testClassReference,328SUSPEND_THREAD, "MethodExitRequest2");329eventRequest2.enable();330break;331332case 2:333eventRequest3 = settingMethodExitRequest (mainThread,334testClassReference,335SUSPEND_ALL, "MethodExitRequest3");336eventRequest3.enable();337break;338339case 3:340eventRequest1 = settingMethodExitRequest (mainThread,341testClassReference,342SUSPEND_NONE, "MethodExitRequest4");343eventRequest1.enable();344eventRequest2 = settingMethodExitRequest (mainThread,345testClassReference,346SUSPEND_THREAD, "MethodExitRequest5");347eventRequest2.enable();348break;349350case 4:351eventRequest1 = settingMethodExitRequest (mainThread,352testClassReference,353SUSPEND_NONE, "MethodExitRequest6");354eventRequest1.enable();355eventRequest3 = settingMethodExitRequest (mainThread,356testClassReference,357SUSPEND_ALL, "MethodExitRequest7");358eventRequest3.enable();359break;360361case 5:362eventRequest2 = settingMethodExitRequest (mainThread,363testClassReference,364SUSPEND_THREAD, "MethodExitRequest8");365eventRequest2.enable();366eventRequest3 = settingMethodExitRequest (mainThread,367testClassReference,368SUSPEND_ALL, "MethodExitRequest9");369eventRequest3.enable();370break;371372case 6:373eventRequest1 = settingMethodExitRequest (mainThread,374testClassReference,375SUSPEND_NONE, "MethodExitRequest10");376eventRequest1.enable();377eventRequest2 = settingMethodExitRequest (mainThread,378testClassReference,379SUSPEND_THREAD, "MethodExitRequest11");380eventRequest2.enable();381eventRequest3 = settingMethodExitRequest (mainThread,382testClassReference,383SUSPEND_ALL, "MethodExitRequest12");384eventRequest3.enable();385break;386387388default:389throw new JDITestRuntimeException("** default case 2 **");390}391392log2("......waiting for new MethodExitEvent : " + i);393mainThread.resume();394getEventSet();395396Event newEvent = eventIterator.nextEvent();397log2(" got new Event with propety 'number' == " + newEvent.request().getProperty("number"));398if ( !( newEvent instanceof MethodExitEvent)) {399log3("ERROR: new event is not MethodExitEvent");400testExitCode = FAILED;401} else {402log2(" Event is instanceof MethodExitEvent");403policy = eventSet.suspendPolicy();404if (policy != policyExpected[i]) {405log3("ERROR: eventSet.suspendPolicy() != policyExpected");406log3(" eventSet.suspendPolicy() == " + policy);407log3(" policyExpected == " + policyExpected[i]);408testExitCode = FAILED;409}410}411412switch (i) {413case 0: eventRequest1.disable(); break;414case 1: eventRequest2.disable(); break;415case 2: eventRequest3.disable(); break;416case 3: eventRequest1.disable(); eventRequest2.disable(); break;417case 4: eventRequest1.disable(); eventRequest3.disable(); break;418case 5: eventRequest2.disable(); eventRequest3.disable(); break;419case 6: eventRequest1.disable(); eventRequest2.disable();420eventRequest3.disable(); break;421}422423switch (policy) {424case SUSPEND_NONE : break;425case SUSPEND_THREAD : mainThread.resume(); break;426case SUSPEND_ALL : vm.resume(); break;427default: throw new JDITestRuntimeException("** default case 3 **");428}429430//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~431}432log1(" TESTING ENDS");433return;434}435436// ============================== test's additional methods437438private MethodExitRequest settingMethodExitRequest ( ThreadReference thread,439ReferenceType testedClass,440int suspendPolicy,441String property )442throws JDITestRuntimeException {443try {444log2("......setting up MethodExitRequest:");445log2(" thread: " + thread + "; class: " + testedClass + "; property: " + property);446447MethodExitRequest448mexr = eventRManager.createMethodExitRequest();449mexr.putProperty("number", property);450mexr.addThreadFilter(thread);451mexr.addClassFilter(testedClass);452mexr.setSuspendPolicy(suspendPolicy);453454log2(" MethodExitRequest has been set up");455return mexr;456} catch ( Exception e ) {457log3("ERROR: ATTENTION: Exception within settingMethodExitRequest() : " + e);458log3(" MethodExitRequest HAS NOT BEEN SET UP");459throw new JDITestRuntimeException("** FAILURE to set up MethodExitRequest **");460}461}462463}464465466