Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy012.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 one more <BR>48* VMDeathRequest with suspendPolicy SUSPEND_EVENT_THREAD. <BR>49* <BR>50* The test has three phases and works as follows. <BR>51* <BR>52* In first phase, <BR>53* upon launching debuggee's VM which will be suspended, <BR>54* a debugger waits for the VMStartEvent within a predefined <BR>55* time interval. If no the VMStartEvent received, the test is FAILED. <BR>56* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>57* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>58* and waits for the event within the predefined time interval. <BR>59* If no the ClassPrepareEvent received, the test is FAILED. <BR>60* Upon getting the ClassPrepareEvent, <BR>61* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>62* within debuggee's special methodForCommunication(). <BR>63* <BR>64* In second phase to check the above, <BR>65* the debugger and the debuggee perform the following loop. <BR>66* - The debugger sets up a VMDeathRequest, resumes <BR>67* the debuggee, and waits for the VMDeathEvent. <BR>68* - The debuggee ends to be resulting in the event. <BR>69* - Upon getting new event, the debugger <BR>70* performs the check corresponding to the event. <BR>71* <BR>72* Note. To inform each other of needed actions, the debugger and <BR>73* and the debuggee use debuggee's variable "instruction". <BR>74* In third phase, at the end, <BR>75* the debuggee changes the value of the "instruction" <BR>76* to inform the debugger of checks finished, and both end. <BR>77* <BR>78*/7980public class suspendpolicy012 extends JDIBase {8182public static void main (String argv[]) {8384int result = run(argv, System.out);8586System.exit(result + PASS_BASE);87}8889public static int run (String argv[], PrintStream out) {9091int exitCode = new suspendpolicy012().runThis(argv, out);9293if (exitCode != PASSED) {94System.out.println("TEST FAILED");95}96return testExitCode;97}9899// ************************************************ test parameters100101private String debuggeeName =102"nsk.jdi.EventSet.suspendPolicy.suspendpolicy012a";103104private String testedClassName =105"nsk.jdi.EventSet.suspendPolicy.TestClass";106107//====================================================== test program108109int policyToCheck = 0;110111private 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;118try {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();304305policyToCheck = policyExpected[1];306mainThread.resume();307308//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~309310log1(" TESTING ENDS");311return;312}313314// ============================== test's additional methods315316private VMDeathRequest settingVMDeathRequest( int suspendPolicy,317String property )318throws JDITestRuntimeException {319try {320log2("......setting up VMDeathRequest:");321log2(" suspendPolicy: " + suspendPolicy + "; property: " + property);322323VMDeathRequest324vmdr = eventRManager.createVMDeathRequest();325vmdr.putProperty("number", property);326vmdr.setSuspendPolicy(suspendPolicy);327328return vmdr;329} catch ( Exception e ) {330log3("ERROR: ATTENTION: Exception within settingVMDeathRequest() : " + e);331log3(" VMDeathRequest HAS NOT BEEN SET UP");332throw new JDITestRuntimeException("** FAILURE to set up a VMDeathRequest **");333}334}335336void check() {337log2("......checking up on eventSet.suspendPolicy()");338int policy = eventSet.suspendPolicy();339if (policy != policyToCheck) {340log3("ERROR: eventSet.suspendPolicy() != policyExpected");341log3(" eventSet.suspendPolicy() == " + policy);342log3(" policyExpected == " + policyToCheck);343testExitCode = FAILED;344}345vm.resume();346}347348}349350351