Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy010.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 StepEvent 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 thread with all possible combinations: <BR>49* NONE, THREAD, ALL <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 a StepRequest, resumes <BR>68* the debuggee, and waits for the StepEvent. <BR>69* - The debuggee invokes the special method 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 suspendpolicy010 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 suspendpolicy010().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.suspendpolicy010a";104105private String testedClassName =106"nsk.jdi.EventSet.suspendPolicy.suspendpolicy010aTestClass";107108//====================================================== test program109110private 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}214215log2("waiting for VMDisconnectEvent");216getEventSet();217if ( !(eventIterator.nextEvent() instanceof VMDisconnectEvent) ) {218log3("ERROR: last event is not the VMDisconnectEvent");219return 1;220}221222return 0;223224} catch ( VMDisconnectedException e ) {225log3("ERROR: VMDisconnectedException : " + e);226return 2;227} catch ( Exception e ) {228log3("ERROR: Exception : " + e);229return 1;230}231232}233234private void testRun()235throws JDITestRuntimeException, Exception {236237eventRManager = vm.eventRequestManager();238239log2("......getting ClassPrepareEvent for debuggee's class");240ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();241cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);242cpRequest.addClassFilter(debuggeeName);243cpRequest.enable();244vm.resume();245getEventSet();246cpRequest.disable();247248ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();249debuggeeClass = event.referenceType();250251if (!debuggeeClass.name().equals(debuggeeName))252throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");253log2(" received: ClassPrepareEvent for debuggeeClass");254255log2("......setting up ClassPrepareEvent for breakpointForCommunication");256257String bPointMethod = "methodForCommunication";258String lineForComm = "lineForComm";259BreakpointRequest bpRequest;260ThreadReference mainThread = debuggee.threadByNameOrThrow("main");261bpRequest = settingBreakpoint(mainThread,262debuggeeClass,263bPointMethod, lineForComm, "zero");264bpRequest.enable();265266vm.resume();267268//------------------------------------------------------ testing section269270log1(" TESTING BEGINS");271272EventRequest eventRequest1 = null;273EventRequest eventRequest2 = null;274EventRequest eventRequest3 = null;275276final int SUSPEND_POLICY = EventRequest.SUSPEND_NONE;277final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;278final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;279final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;280281int policyExpected[] = { SUSPEND_NONE,282SUSPEND_THREAD,283SUSPEND_ALL,284SUSPEND_THREAD,285SUSPEND_ALL,286SUSPEND_ALL,287SUSPEND_ALL };288int policy = 0;289290291for (int i = 0; ; i++) {292293breakpointForCommunication();294295int instruction = ((IntegerValue)296(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();297if (instruction == 0) {298vm.resume();299break;300}301302log1(":::::: case: # " + i);303304//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part305306switch (i) {307308case 0:309eventRequest1 = settingStepRequest (mainThread,310SUSPEND_NONE, "StepRequest1");311eventRequest1.enable();312break;313314case 1:315eventRequest2 = settingStepRequest (mainThread,316SUSPEND_THREAD, "StepRequest2");317eventRequest2.enable();318break;319320case 2:321eventRequest3 = settingStepRequest (mainThread,322323SUSPEND_ALL, "StepRequest3");324eventRequest3.enable();325break;326327328default:329throw new JDITestRuntimeException("** default case 2 **");330}331332mainThread.resume();333getEventSet();334335if ( !(eventIterator.nextEvent() instanceof StepEvent)) {336log3("ERROR: new event is not StepEvent");337testExitCode = FAILED;338} else {339log2("......got : instanceof StepEvent");340policy = eventSet.suspendPolicy();341if (policy != policyExpected[i]) {342log3("ERROR: eventSet.suspendPolicy() != policyExpected");343log3(" eventSet.suspendPolicy() == " + policy);344log3(" policyExpected == " + policyExpected[i]);345testExitCode = FAILED;346}347}348349switch (i) {350case 0: eventRequest1.disable(); break;351case 1: eventRequest2.disable(); break;352case 2: eventRequest3.disable(); break;353}354355switch (policy) {356case SUSPEND_NONE : break;357case SUSPEND_THREAD : mainThread.resume(); break;358case SUSPEND_ALL : vm.resume(); break;359default: throw new JDITestRuntimeException("** default case 3 **");360}361362//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~363}364log1(" TESTING ENDS");365return;366}367368// ============================== test's additional methods369370private StepRequest settingStepRequest ( ThreadReference thread,371int suspendPolicy,372String property )373throws JDITestRuntimeException {374try {375log2("......setting up StepRequest:");376log2(" thread: " + thread + "; property: " + property);377378StepRequest379str = eventRManager.createStepRequest(thread, StepRequest.STEP_LINE, StepRequest.STEP_OVER);380str.putProperty("number", property);381str.addCountFilter(1);382str.setSuspendPolicy(suspendPolicy);383384log2(" StepRequest has been set up");385return str;386} catch ( Exception e ) {387log3("ERROR: ATTENTION: Exception within settingStepRequest() : " + e);388log3(" StepRequest HAS NOT BEEN SET UP");389throw new JDITestRuntimeException("** FAILURE to set up StepRequest **");390}391}392393}394395396