Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy006.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 MethodEntryEvent 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 a MethodEntryRequest, resumes <BR>69* the debuggee, and waits for the MethodEntryEvent. <BR>70* - The debuggee invokes the special method to be resulting in the event.<BR>71* - Upon getting new event, the debugger <BR>72* performs the check corresponding to the event. <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 suspendpolicy006 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 suspendpolicy006().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.suspendpolicy006a";105106private String testedClassName =107"nsk.jdi.EventSet.suspendPolicy.suspendpolicy006aTestClass";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;290291292ReferenceType testClassReference = null;293294String fieldName = "var1";295296297for (int i = 0; ; i++) {298299300breakpointForCommunication();301302int instruction = ((IntegerValue)303(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();304if (instruction == 0) {305vm.resume();306break;307}308309310log1(":::::: case: # " + i);311312//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part313314switch (i) {315316case 0:317testClassReference =318(ReferenceType) vm.classesByName(testedClassName).get(0);319320eventRequest1 = settingMethodEntryRequest (mainThread,321testClassReference,322SUSPEND_NONE, "MethodEntryRequest1");323eventRequest1.enable();324break;325326case 1:327eventRequest2 = settingMethodEntryRequest (mainThread,328testClassReference,329SUSPEND_THREAD, "MethodEntryRequest2");330eventRequest2.enable();331break;332333case 2:334eventRequest3 = settingMethodEntryRequest (mainThread,335testClassReference,336SUSPEND_ALL, "MethodEntryRequest3");337eventRequest3.enable();338break;339340case 3:341eventRequest1 = settingMethodEntryRequest (mainThread,342testClassReference,343SUSPEND_NONE, "MethodEntryRequest4");344eventRequest1.enable();345eventRequest2 = settingMethodEntryRequest (mainThread,346testClassReference,347SUSPEND_THREAD, "MethodEntryRequest5");348eventRequest2.enable();349break;350351case 4:352eventRequest1 = settingMethodEntryRequest (mainThread,353testClassReference,354SUSPEND_NONE, "MethodEntryRequest6");355eventRequest1.enable();356eventRequest3 = settingMethodEntryRequest (mainThread,357testClassReference,358SUSPEND_ALL, "MethodEntryRequest7");359eventRequest3.enable();360break;361362case 5:363eventRequest2 = settingMethodEntryRequest (mainThread,364testClassReference,365SUSPEND_THREAD, "MethodEntryRequest8");366eventRequest2.enable();367eventRequest3 = settingMethodEntryRequest (mainThread,368testClassReference,369SUSPEND_ALL, "MethodEntryRequest9");370eventRequest3.enable();371break;372373case 6:374eventRequest1 = settingMethodEntryRequest (mainThread,375testClassReference,376SUSPEND_NONE, "MethodEntryRequest10");377eventRequest1.enable();378eventRequest2 = settingMethodEntryRequest (mainThread,379testClassReference,380SUSPEND_THREAD, "MethodEntryRequest11");381eventRequest2.enable();382eventRequest3 = settingMethodEntryRequest (mainThread,383testClassReference,384SUSPEND_ALL, "MethodEntryRequest12");385eventRequest3.enable();386break;387388389default:390throw new JDITestRuntimeException("** default case 2 **");391}392393log2("......waiting for new MethodEntryEvent : " + i);394mainThread.resume();395getEventSet();396397Event newEvent = eventIterator.nextEvent();398log2(" got new Event with propety 'number' == " + newEvent.request().getProperty("number"));399if ( !( newEvent instanceof MethodEntryEvent)) {400log3("ERROR: new event is not MethodEntryEvent");401testExitCode = FAILED;402} else {403log2(" Event is instanceof MethodEntryEvent");404policy = eventSet.suspendPolicy();405if (policy != policyExpected[i]) {406log3("ERROR: eventSet.suspendPolicy() != policyExpected");407log3(" eventSet.suspendPolicy() == " + policy);408log3(" policyExpected == " + policyExpected[i]);409testExitCode = FAILED;410}411}412413switch (i) {414case 0: eventRequest1.disable(); break;415case 1: eventRequest2.disable(); break;416case 2: eventRequest3.disable(); break;417case 3: eventRequest1.disable(); eventRequest2.disable(); break;418case 4: eventRequest1.disable(); eventRequest3.disable(); break;419case 5: eventRequest2.disable(); eventRequest3.disable(); break;420case 6: eventRequest1.disable(); eventRequest2.disable();421eventRequest3.disable(); break;422}423424switch (policy) {425case SUSPEND_NONE : break;426case SUSPEND_THREAD : mainThread.resume(); break;427case SUSPEND_ALL : vm.resume(); break;428default: throw new JDITestRuntimeException("** default case 3 **");429}430431//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~432}433log1(" TESTING ENDS");434return;435}436437// ============================== test's additional methods438439private MethodEntryRequest settingMethodEntryRequest ( ThreadReference thread,440ReferenceType testedClass,441int suspendPolicy,442String property )443throws JDITestRuntimeException {444try {445log2("......setting up MethodEntryRequest:");446log2(" thread: " + thread + "; class: " + testedClass + "; property: " + property);447448MethodEntryRequest449menr = eventRManager.createMethodEntryRequest();450menr.putProperty("number", property);451menr.addThreadFilter(thread);452menr.addClassFilter(testedClass);453menr.setSuspendPolicy(suspendPolicy);454455log2(" a MethodEntryRequest has been set up");456return menr;457} catch ( Exception e ) {458log3("ERROR: ATTENTION: Exception within settingMethodEntryRequest() : " + e);459log3(" MethodEntryRequest HAS NOT BEEN SET UP");460throw new JDITestRuntimeException("** FAILURE to set up MethodEntryRequest **");461}462}463464}465466467