Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy016.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 VMDeathEvent 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 set containing two more <BR>48* VMDeathRequests with suspendPolicies <BR>49* SUSPEND_ALL and SUSPEND_EVENT_THREAD. <BR>50* <BR>51* The test has three phases and works as follows. <BR>52* <BR>53* In first phase, <BR>54* upon launching debuggee's VM which will be suspended, <BR>55* a debugger waits for the VMStartEvent within a predefined <BR>56* time interval. If no the VMStartEvent received, the test is FAILED. <BR>57* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>58* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>59* and waits for the event within the predefined time interval. <BR>60* If no the ClassPrepareEvent received, the test is FAILED. <BR>61* Upon getting the ClassPrepareEvent, <BR>62* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>63* within debuggee's special methodForCommunication(). <BR>64* <BR>65* In second phase to check the above, <BR>66* the debugger and the debuggee perform the following loop. <BR>67* - The debugger sets up VMDeathRequests, resumes <BR>68* the debuggee, and waits for the VMDeathEvent. <BR>69* - The debuggee ends to be resulting in the event. <BR>70* - Upon getting new event, the debugger <BR>71* performs the check corresponding to the event. <BR>72* <BR>73* Note. To inform each other of needed actions, the debugger and <BR>74* and the debuggee use debuggee's variable "instruction". <BR>75* In third phase, at the end, <BR>76* the debuggee changes the value of the "instruction" <BR>77* to inform the debugger of checks finished, and both end. <BR>78* <BR>79*/8081public class suspendpolicy016 extends JDIBase {8283public static void main (String argv[]) {8485int result = run(argv, System.out);8687System.exit(result + PASS_BASE);88}8990public static int run (String argv[], PrintStream out) {9192int exitCode = new suspendpolicy016().runThis(argv, out);9394if (exitCode != PASSED) {95System.out.println("TEST FAILED");96}97return testExitCode;98}99100private String debuggeeName =101"nsk.jdi.EventSet.suspendPolicy.suspendpolicy016a";102103private String testedClassName =104"nsk.jdi.EventSet.suspendPolicy.TestClass";105106//====================================================== test program107108int policyToCheck = 0;109110private int runThis (String argv[], PrintStream out) {111112argsHandler = new ArgumentHandler(argv);113logHandler = new Log(out, argsHandler);114Binder binder = new Binder(argsHandler, logHandler);115116waitTime = argsHandler.getWaitTime() * 60000;117118try {119log2("launching a debuggee :");120log2(" " + debuggeeName);121if (argsHandler.verbose()) {122debuggee = binder.bindToDebugee(debuggeeName + " -vbs");123} else {124debuggee = binder.bindToDebugee(debuggeeName);125}126if (debuggee == null) {127log3("ERROR: no debuggee launched");128return FAILED;129}130log2("debuggee launched");131} catch ( Exception e ) {132log3("ERROR: Exception : " + e);133log2(" test cancelled");134return FAILED;135}136137debuggee.redirectOutput(logHandler);138139vm = debuggee.VM();140141eventQueue = vm.eventQueue();142if (eventQueue == null) {143log3("ERROR: eventQueue == null : TEST ABORTED");144vm.exit(PASS_BASE);145return FAILED;146}147148log2("invocation of the method runTest()");149switch (runTest()) {150151case 0 : log2("test phase has finished normally");152log2(" waiting for the debuggee to finish ...");153debuggee.waitFor();154155log2("......getting the debuggee's exit status");156int status = debuggee.getStatus();157if (status != PASS_BASE) {158log3("ERROR: debuggee returned UNEXPECTED exit status: " +159status + " != PASS_BASE");160testExitCode = FAILED;161} else {162log2("......debuggee returned expected exit status: " +163status + " == PASS_BASE");164}165break;166167default : log3("ERROR: runTest() returned unexpected value");168169case 1 : log3("test phase has not finished normally: debuggee is still alive");170log2("......forcing: vm.exit();");171testExitCode = FAILED;172try {173vm.exit(PASS_BASE);174} catch ( Exception e ) {175log3("ERROR: Exception : e");176}177break;178179case 2 : log3("test cancelled due to VMDisconnectedException");180log2("......trying: vm.process().destroy();");181testExitCode = FAILED;182try {183Process vmProcess = vm.process();184if (vmProcess != null) {185vmProcess.destroy();186}187} catch ( Exception e ) {188log3("ERROR: Exception : e");189}190break;191}192193return testExitCode;194}195196197/*198* Return value: 0 - normal end of the test199* 1 - ubnormal end of the test200* 2 - VMDisconnectedException while test phase201*/202203private int runTest() {204205try {206testRun();207208log2("waiting for VMDeathEvent");209getEventSet();210if ( !(eventIterator.nextEvent() instanceof VMDeathEvent) ) {211log3("ERROR: last event is not the VMDeathEvent");212return 1;213}214check();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 {237238if ( !vm.canRequestVMDeathEvent() ) {239log2("......vm.canRequestVMDeathEvent == false :: test cancelled");240vm.exit(PASS_BASE);241return;242}243244245eventRManager = vm.eventRequestManager();246247log2("......getting ClassPrepareEvent for debuggee's class");248ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();249cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);250cpRequest.addClassFilter(debuggeeName);251cpRequest.enable();252vm.resume();253getEventSet();254cpRequest.disable();255256ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();257debuggeeClass = event.referenceType();258259if (!debuggeeClass.name().equals(debuggeeName))260throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");261log2(" received: ClassPrepareEvent for debuggeeClass");262263log2("......setting up ClassPrepareEvent for breakpointForCommunication");264265String bPointMethod = "methodForCommunication";266String lineForComm = "lineForComm";267BreakpointRequest bpRequest;268ThreadReference mainThread = debuggee.threadByNameOrThrow("main");269bpRequest = settingBreakpoint(mainThread,270debuggeeClass,271bPointMethod, lineForComm, "zero");272bpRequest.enable();273274vm.resume();275276//------------------------------------------------------ testing section277278log1(" TESTING BEGINS");279280EventRequest eventRequest1 = null;281EventRequest eventRequest2 = null;282EventRequest eventRequest3 = null;283284final int SUSPEND_POLICY = EventRequest.SUSPEND_NONE;285final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;286final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;287final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;288289int policyExpected[] = { SUSPEND_NONE,290SUSPEND_THREAD,291SUSPEND_ALL,292SUSPEND_THREAD,293SUSPEND_ALL,294SUSPEND_ALL,295SUSPEND_ALL };296int policy = 0;297298breakpointForCommunication();299300//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part301302eventRequest2 = settingVMDeathRequest (SUSPEND_THREAD, "VMDeathRequest2");303eventRequest2.enable();304eventRequest3 = settingVMDeathRequest (SUSPEND_ALL, "VMDeathRequest3");305eventRequest3.enable();306307policyToCheck = policyExpected[5];308mainThread.resume();309310//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~311312log1(" TESTING ENDS");313return;314}315316317// ============================== test's additional methods318319private VMDeathRequest settingVMDeathRequest( int suspendPolicy,320String property )321throws JDITestRuntimeException {322try {323log2("......setting up VMDeathRequest:");324log2(" suspendPolicy: " + suspendPolicy + "; property: " + property);325326VMDeathRequest327vmdr = eventRManager.createVMDeathRequest();328vmdr.putProperty("number", property);329vmdr.setSuspendPolicy(suspendPolicy);330331return vmdr;332} catch ( Exception e ) {333log3("ERROR: ATTENTION: Exception within settingVMDeathRequest() : " + e);334log3(" VMDeathRequest HAS NOT BEEN SET UP");335throw new JDITestRuntimeException("** FAILURE to set up a VMDeathRequest **");336}337}338339void check() {340log2("......checking up on eventSet.suspendPolicy()");341int policy = eventSet.suspendPolicy();342if (policy != policyToCheck) {343log3("ERROR: eventSet.suspendPolicy() != policyExpected");344log3(" eventSet.suspendPolicy() == " + policy);345log3(" policyExpected == " + policyToCheck);346testExitCode = FAILED;347}348vm.resume();349}350351}352353354