Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy011.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 <BR>48* one more VMDeathRequest with suspendPolicy SUSPEND_NONE. <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 suspendpolicy011 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 suspendpolicy011().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.suspendpolicy011a";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;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}215check();216217log2("waiting for VMDisconnectEvent");218getEventSet();219if ( !(eventIterator.nextEvent() instanceof VMDisconnectEvent) ) {220log3("ERROR: last event is not the VMDisconnectEvent");221return 1;222}223224return 0;225226} catch ( VMDisconnectedException e ) {227log3("ERROR: VMDisconnectedException : " + e);228return 2;229} catch ( Exception e ) {230log3("ERROR: Exception : " + e);231return 1;232}233234}235236private void testRun()237throws JDITestRuntimeException, Exception {238239if ( !vm.canRequestVMDeathEvent() ) {240log2("......vm.canRequestVMDeathEvent == false :: test cancelled");241vm.exit(PASS_BASE);242return;243}244245246eventRManager = vm.eventRequestManager();247248log2("......getting ClassPrepareEvent for debuggee's class");249ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();250cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);251cpRequest.addClassFilter(debuggeeName);252cpRequest.enable();253vm.resume();254getEventSet();255cpRequest.disable();256257ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();258debuggeeClass = event.referenceType();259260if (!debuggeeClass.name().equals(debuggeeName))261throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");262log2(" received: ClassPrepareEvent for debuggeeClass");263264log2("......setting up ClassPrepareEvent for breakpointForCommunication");265266String bPointMethod = "methodForCommunication";267String lineForComm = "lineForComm";268BreakpointRequest bpRequest;269ThreadReference mainThread = debuggee.threadByNameOrThrow("main");270bpRequest = settingBreakpoint(mainThread,271debuggeeClass,272bPointMethod, lineForComm, "zero");273bpRequest.enable();274275vm.resume();276277//------------------------------------------------------ testing section278279log1(" TESTING BEGINS");280281EventRequest eventRequest1 = null;282EventRequest eventRequest2 = null;283EventRequest eventRequest3 = null;284285final int SUSPEND_POLICY = EventRequest.SUSPEND_NONE;286final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;287final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;288final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;289290int policyExpected[] = { SUSPEND_NONE,291SUSPEND_THREAD,292SUSPEND_ALL,293SUSPEND_THREAD,294SUSPEND_ALL,295SUSPEND_ALL,296SUSPEND_ALL };297int policy = 0;298299breakpointForCommunication();300301//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part302303eventRequest1 = settingVMDeathRequest ( SUSPEND_NONE, "VMDeathRequest1");304eventRequest1.enable();305306policyToCheck = policyExpected[0];307mainThread.resume();308309//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~310311log1(" TESTING ENDS");312return;313}314315316// ============================== test's additional methods317318private VMDeathRequest settingVMDeathRequest( int suspendPolicy,319String property )320throws JDITestRuntimeException {321try {322log2("......setting up VMDeathRequest:");323log2(" suspendPolicy: " + suspendPolicy + "; property: " + property);324325VMDeathRequest326vmdr = eventRManager.createVMDeathRequest();327vmdr.putProperty("number", property);328vmdr.setSuspendPolicy(suspendPolicy);329330return vmdr;331} catch ( Exception e ) {332log3("ERROR: ATTENTION: Exception within settingVMDeathRequest() : " + e);333log3(" VMDeathRequest HAS NOT BEEN SET UP");334throw new JDITestRuntimeException("** FAILURE to set up a VMDeathRequest **");335}336}337338void check() {339log2("......checking up on eventSet.suspendPolicy()");340int policy = eventSet.suspendPolicy();341if (policy != policyToCheck) {342log3("ERROR: eventSet.suspendPolicy() != policyExpected");343log3(" eventSet.suspendPolicy() == " + policy);344log3(" policyExpected == " + policyToCheck);345testExitCode = FAILED;346}347vm.resume();348}349350}351352353