Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy009.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 ThreadDeathEvent 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 to three threads with all possible combinations: <BR>49* NONE, THREAD, ALL, NONE+THREAD, NONE+ALL, <BR>50* THREAD+ALL, NONE+THREAD+ALL <BR>51* <BR>52* The test has three phases and works as follows. <BR>53* <BR>54* In first phase, <BR>55* upon launching debuggee's VM which will be suspended, <BR>56* a debugger waits for the VMStartEvent within a predefined <BR>57* time interval. If no the VMStartEvent received, the test is FAILED. <BR>58* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>59* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>60* and waits for the event within the predefined time interval. <BR>61* If no the ClassPrepareEvent received, the test is FAILED. <BR>62* Upon getting the ClassPrepareEvent, <BR>63* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>64* within debuggee's special methodForCommunication(). <BR>65* <BR>66* In second phase to check the above, <BR>67* the debugger and the debuggee perform the following loop. <BR>68* - The debugger sets up one to three new ThreadDeathRequests, <BR>69* resumes the debuggee, and waits for the ThreadDeathEvents. <BR>70* - The debuggee starts new thread to be resulting in the events. <BR>71* - Upon getting new ThreadDeathEvent, <BR>72* the debugger performs the check corresponding to the EventSet. <BR>73* <BR>74* Note. To inform each other of needed actions, the debugger and <BR>75* and the debuggee use debuggee's variable "instruction". <BR>76* In third phase, at the end, <BR>77* the debuggee changes the value of the "instruction" <BR>78* to inform the debugger of checks finished, and both end. <BR>79* <BR>80*/8182public class suspendpolicy009 extends JDIBase {8384public static void main (String argv[]) {8586int result = run(argv, System.out);8788System.exit(result + PASS_BASE);89}9091public static int run (String argv[], PrintStream out) {9293int exitCode = new suspendpolicy009().runThis(argv, out);9495if (exitCode != PASSED) {96System.out.println("TEST FAILED");97}98return testExitCode;99}100101// ************************************************ test parameters102103private String debuggeeName =104"nsk.jdi.EventSet.suspendPolicy.suspendpolicy009a";105106private String testedClassName =107"nsk.jdi.EventSet.suspendPolicy.TestClass";108109//====================================================== test program110111private 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}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 {237238eventRManager = vm.eventRequestManager();239240log2("......getting ClassPrepareEvent for debuggee's class");241ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();242cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);243cpRequest.addClassFilter(debuggeeName);244cpRequest.enable();245vm.resume();246getEventSet();247cpRequest.disable();248249ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();250debuggeeClass = event.referenceType();251252if (!debuggeeClass.name().equals(debuggeeName))253throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");254log2(" received: ClassPrepareEvent for debuggeeClass");255256log2("......setting up ClassPrepareEvent for breakpointForCommunication");257258String bPointMethod = "methodForCommunication";259String lineForComm = "lineForComm";260BreakpointRequest bpRequest;261ThreadReference mainThread = debuggee.threadByNameOrThrow("main");262bpRequest = settingBreakpoint(mainThread,263debuggeeClass,264bPointMethod, lineForComm, "zero");265bpRequest.enable();266267vm.resume();268269//------------------------------------------------------ testing section270271log1(" TESTING BEGINS");272273EventRequest eventRequest1 = null;274EventRequest eventRequest2 = null;275EventRequest eventRequest3 = null;276277final int SUSPEND_POLICY = EventRequest.SUSPEND_NONE;278final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;279final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;280final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;281282int policyExpected[] = { SUSPEND_NONE,283SUSPEND_THREAD,284SUSPEND_ALL,285SUSPEND_THREAD,286SUSPEND_ALL,287SUSPEND_ALL,288SUSPEND_ALL };289int policy = 0;290291292for (int i = 0; ; i++) {293294breakpointForCommunication();295296int instruction = ((IntegerValue)297(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();298if (instruction == 0) {299vm.resume();300break;301}302303304log1(":::::: case: # " + i);305306//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part307308switch (i) {309310case 0:311eventRequest1 = settingThreadDeathRequest (312SUSPEND_NONE, "ThreadDeathRequest1");313eventRequest1.enable();314break;315316case 1:317eventRequest2 = settingThreadDeathRequest (318SUSPEND_THREAD, "ThreadDeathRequest2");319eventRequest2.enable();320break;321322case 2:323eventRequest3 = settingThreadDeathRequest (324SUSPEND_ALL, "ThreadDeathRequest3");325eventRequest3.enable();326break;327328case 3:329eventRequest1 = settingThreadDeathRequest (330SUSPEND_NONE, "ThreadDeathRequest1");331eventRequest1.enable();332eventRequest2 = settingThreadDeathRequest (333SUSPEND_THREAD, "ThreadDeathRequest2");334eventRequest2.enable();335break;336337case 4:338eventRequest1 = settingThreadDeathRequest (339SUSPEND_NONE, "ThreadDeathRequest1");340eventRequest1.enable();341eventRequest3 = settingThreadDeathRequest (342SUSPEND_ALL, "ThreadDeathRequest3");343eventRequest3.enable();344break;345346case 5:347eventRequest2 = settingThreadDeathRequest (348SUSPEND_THREAD, "ThreadDeathRequest2");349eventRequest2.enable();350eventRequest3 = settingThreadDeathRequest (351SUSPEND_ALL, "ThreadDeathRequest3");352eventRequest3.enable();353break;354355case 6:356eventRequest1 = settingThreadDeathRequest (357SUSPEND_NONE, "ThreadDeathRequest1");358eventRequest1.enable();359eventRequest2 = settingThreadDeathRequest (360SUSPEND_THREAD, "ThreadDeathRequest2");361eventRequest2.enable();362eventRequest3 = settingThreadDeathRequest (363SUSPEND_ALL, "ThreadDeathRequest3");364eventRequest3.enable();365break;366367368default:369throw new JDITestRuntimeException("** default case 2 **");370}371372mainThread.resume();373getEventSet();374375if ( !(eventIterator.nextEvent() instanceof ThreadDeathEvent)) {376log3("ERROR: new event is not ThreadDeathEvent");377testExitCode = FAILED;378} else {379log2("......got : instanceof ThreadDeathEvent");380policy = eventSet.suspendPolicy();381if (policy != policyExpected[i]) {382log3("ERROR: eventSet.suspendPolicy() != policyExpected");383log3(" eventSet.suspendPolicy() == " + policy);384log3(" policyExpected == " + policyExpected[i]);385testExitCode = FAILED;386}387}388389switch (i) {390case 0: eventRequest1.disable(); break;391case 1: eventRequest2.disable(); break;392case 2: eventRequest3.disable(); break;393case 3: eventRequest1.disable(); eventRequest2.disable(); break;394case 4: eventRequest1.disable(); eventRequest3.disable(); break;395case 5: eventRequest2.disable(); eventRequest3.disable(); break;396case 6: eventRequest1.disable(); eventRequest2.disable();397eventRequest3.disable(); break;398}399400switch (policy) {401case SUSPEND_NONE : break;402case SUSPEND_THREAD : vm.resume(); break;403case SUSPEND_ALL : vm.resume(); break;404default: throw new JDITestRuntimeException("** default case 3 **");405}406407//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~408}409log1(" TESTING ENDS");410return;411}412413// ============================== test's additional methods414415private ThreadDeathRequest settingThreadDeathRequest(int suspendPolicy,416String property)417throws JDITestRuntimeException {418try {419ThreadDeathRequest tsr = eventRManager.createThreadDeathRequest();420// tsr.addThreadFilter(mainThread);421tsr.addCountFilter(1);422tsr.setSuspendPolicy(suspendPolicy);423tsr.putProperty("number", property);424return tsr;425} catch ( Exception e ) {426log3("ERROR: ATTENTION: Exception within settingThreadDeathRequest() : " + e);427log3(" ThreadDeathRequest HAS NOT BEEN SET UP");428throw new JDITestRuntimeException("** FAILURE to set up ThreadDeathRequest **");429}430}431432}433434435