Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy015.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_NONE. <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 suspendpolicy015 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 suspendpolicy015().runThis(argv, out);9394if (exitCode != PASSED) {95System.out.println("TEST FAILED");96}97return testExitCode;98}99100// ************************************************ test parameters101102private String debuggeeName =103"nsk.jdi.EventSet.suspendPolicy.suspendpolicy015a";104105private String testedClassName =106"nsk.jdi.EventSet.suspendPolicy.TestClass";107108//====================================================== test program109110int policyToCheck = 0;111112private int runThis (String argv[], PrintStream out) {113114argsHandler = new ArgumentHandler(argv);115logHandler = new Log(out, argsHandler);116Binder binder = new Binder(argsHandler, logHandler);117118waitTime = argsHandler.getWaitTime() * 60000;119120try {121log2("launching a debuggee :");122log2(" " + debuggeeName);123if (argsHandler.verbose()) {124debuggee = binder.bindToDebugee(debuggeeName + " -vbs");125} else {126debuggee = binder.bindToDebugee(debuggeeName);127}128if (debuggee == null) {129log3("ERROR: no debuggee launched");130return FAILED;131}132log2("debuggee launched");133} catch ( Exception e ) {134log3("ERROR: Exception : " + e);135log2(" test cancelled");136return FAILED;137}138139debuggee.redirectOutput(logHandler);140141vm = debuggee.VM();142143eventQueue = vm.eventQueue();144if (eventQueue == null) {145log3("ERROR: eventQueue == null : TEST ABORTED");146vm.exit(PASS_BASE);147return FAILED;148}149150log2("invocation of the method runTest()");151switch (runTest()) {152153case 0 : log2("test phase has finished normally");154log2(" waiting for the debuggee to finish ...");155debuggee.waitFor();156157log2("......getting the debuggee's exit status");158int status = debuggee.getStatus();159if (status != PASS_BASE) {160log3("ERROR: debuggee returned UNEXPECTED exit status: " +161status + " != PASS_BASE");162testExitCode = FAILED;163} else {164log2("......debuggee returned expected exit status: " +165status + " == PASS_BASE");166}167break;168169default : log3("ERROR: runTest() returned unexpected value");170171case 1 : log3("test phase has not finished normally: debuggee is still alive");172log2("......forcing: vm.exit();");173testExitCode = FAILED;174try {175vm.exit(PASS_BASE);176} catch ( Exception e ) {177log3("ERROR: Exception : e");178}179break;180181case 2 : log3("test cancelled due to VMDisconnectedException");182log2("......trying: vm.process().destroy();");183testExitCode = FAILED;184try {185Process vmProcess = vm.process();186if (vmProcess != null) {187vmProcess.destroy();188}189} catch ( Exception e ) {190log3("ERROR: Exception : e");191}192break;193}194195return testExitCode;196}197198199/*200* Return value: 0 - normal end of the test201* 1 - ubnormal end of the test202* 2 - VMDisconnectedException while test phase203*/204205private int runTest() {206207try {208testRun();209210log2("waiting for VMDeathEvent");211getEventSet();212if ( !(eventIterator.nextEvent() instanceof VMDeathEvent) ) {213log3("ERROR: last event is not the VMDeathEvent");214return 1;215}216check();217218log2("waiting for VMDisconnectEvent");219getEventSet();220if ( !(eventIterator.nextEvent() instanceof VMDisconnectEvent) ) {221log3("ERROR: last event is not the VMDisconnectEvent");222return 1;223}224225return 0;226227} catch ( VMDisconnectedException e ) {228log3("ERROR: VMDisconnectedException : " + e);229return 2;230} catch ( Exception e ) {231log3("ERROR: Exception : " + e);232return 1;233}234235}236237private void testRun()238throws JDITestRuntimeException, Exception {239240if ( !vm.canRequestVMDeathEvent() ) {241log2("......vm.canRequestVMDeathEvent == false :: test cancelled");242vm.exit(PASS_BASE);243return;244}245246247eventRManager = vm.eventRequestManager();248249log2("......getting ClassPrepareEvent for debuggee's class");250ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();251cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);252cpRequest.addClassFilter(debuggeeName);253cpRequest.enable();254vm.resume();255getEventSet();256cpRequest.disable();257258ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();259debuggeeClass = event.referenceType();260261if (!debuggeeClass.name().equals(debuggeeName))262throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");263log2(" received: ClassPrepareEvent for debuggeeClass");264265log2("......setting up ClassPrepareEvent for breakpointForCommunication");266267String bPointMethod = "methodForCommunication";268String lineForComm = "lineForComm";269BreakpointRequest bpRequest;270ThreadReference mainThread = debuggee.threadByNameOrThrow("main");271bpRequest = settingBreakpoint(mainThread,272debuggeeClass,273bPointMethod, lineForComm, "zero");274bpRequest.enable();275276vm.resume();277278//------------------------------------------------------ testing section279280log1(" TESTING BEGINS");281282EventRequest eventRequest1 = null;283EventRequest eventRequest2 = null;284EventRequest eventRequest3 = null;285286final int SUSPEND_POLICY = EventRequest.SUSPEND_NONE;287final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;288final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;289final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;290291int policyExpected[] = { SUSPEND_NONE,292SUSPEND_THREAD,293SUSPEND_ALL,294SUSPEND_THREAD,295SUSPEND_ALL,296SUSPEND_ALL,297SUSPEND_ALL };298int policy = 0;299300breakpointForCommunication();301302//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part303304eventRequest1 = settingVMDeathRequest (SUSPEND_NONE, "VMDeathRequest1");305eventRequest1.enable();306eventRequest3 = settingVMDeathRequest (SUSPEND_ALL, "VMDeathRequest3");307eventRequest3.enable();308309policyToCheck = policyExpected[4];310mainThread.resume();311312//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~313314log1(" TESTING ENDS");315return;316}317318// ============================== test's additional methods319320private VMDeathRequest settingVMDeathRequest( int suspendPolicy,321String property )322throws JDITestRuntimeException {323try {324log2("......setting up VMDeathRequest:");325log2(" suspendPolicy: " + suspendPolicy + "; property: " + property);326327VMDeathRequest328vmdr = eventRManager.createVMDeathRequest();329vmdr.putProperty("number", property);330vmdr.setSuspendPolicy(suspendPolicy);331332return vmdr;333} catch ( Exception e ) {334log3("ERROR: ATTENTION: Exception within settingVMDeathRequest() : " + e);335log3(" VMDeathRequest HAS NOT BEEN SET UP");336throw new JDITestRuntimeException("** FAILURE to set up a VMDeathRequest **");337}338}339340void check() {341log2("......checking up on eventSet.suspendPolicy()");342int policy = eventSet.suspendPolicy();343if (policy != policyToCheck) {344log3("ERROR: eventSet.suspendPolicy() != policyExpected");345log3(" eventSet.suspendPolicy() == " + policy);346log3(" policyExpected == " + policyToCheck);347testExitCode = FAILED;348}349vm.resume();350}351352}353354355