Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy005.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 an ExceptionEvent set, <BR>45* the method returns a value 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 ExceptionRequest, <BR>69* resumes the debuggee, and waits for the ExceptionEvent. <BR>70* - The debuggee invokes the special method in which <BR>71* NullPointerException is thrown to be resulting in the event. <BR>72* - Upon getting new event, the debugger <BR>73* performs the check corresponding to the event. <BR>74* <BR>75* Note. To inform each other of needed actions, the debugger and <BR>76* and the debuggee use debuggee's variable "instruction". <BR>77* In third phase, at the end, <BR>78* the debuggee changes the value of the "instruction" <BR>79* to inform the debugger of checks finished, and both end. <BR>80* <BR>81*/8283public class suspendpolicy005 extends JDIBase {8485public static void main (String argv[]) {8687int result = run(argv, System.out);8889System.exit(result + PASS_BASE);90}9192public static int run (String argv[], PrintStream out) {9394int exitCode = new suspendpolicy005().runThis(argv, out);9596if (exitCode != PASSED) {97System.out.println("TEST FAILED");98}99return testExitCode;100}101102// ************************************************ test parameters103104private String debuggeeName =105"nsk.jdi.EventSet.suspendPolicy.suspendpolicy005a";106107private String testedClassName =108"nsk.jdi.EventSet.suspendPolicy.suspendpolicy005aTestClass";109110//====================================================== test program111112private 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}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 {238239eventRManager = vm.eventRequestManager();240241log2("......getting ClassPrepareEvent for debuggee's class");242ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();243cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);244cpRequest.addClassFilter(debuggeeName);245cpRequest.enable();246vm.resume();247getEventSet();248cpRequest.disable();249250ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();251debuggeeClass = event.referenceType();252253if (!debuggeeClass.name().equals(debuggeeName))254throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");255log2(" received: ClassPrepareEvent for debuggeeClass");256257log2("......setting up ClassPrepareEvent for breakpointForCommunication");258259String bPointMethod = "methodForCommunication";260String lineForComm = "lineForComm";261BreakpointRequest bpRequest;262ThreadReference mainThread = debuggee.threadByNameOrThrow("main");263bpRequest = settingBreakpoint(mainThread,264debuggeeClass,265bPointMethod, lineForComm, "zero");266bpRequest.enable();267268vm.resume();269270//------------------------------------------------------ testing section271272log1(" TESTING BEGINS");273274EventRequest eventRequest1 = null;275EventRequest eventRequest2 = null;276EventRequest eventRequest3 = null;277278final int SUSPEND_POLICY = EventRequest.SUSPEND_NONE;279final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;280final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;281final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;282283int policyExpected[] = { SUSPEND_NONE,284SUSPEND_THREAD,285SUSPEND_ALL,286SUSPEND_THREAD,287SUSPEND_ALL,288SUSPEND_ALL,289SUSPEND_ALL };290int policy = 0;291292293ReferenceType testClassReference = null;294295String methodName = "method";296String bpLineName = "breakpointLine";297298299for (int i = 0; ; i++) {300301302breakpointForCommunication();303304int instruction = ((IntegerValue)305(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();306if (instruction == 0) {307vm.resume();308break;309}310311312log1(":::::: case: # " + i);313314//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part315316switch (i) {317318case 0:319testClassReference =320(ReferenceType) vm.classesByName(testedClassName).get(0);321322eventRequest1 = settingExceptionRequest (mainThread,323debuggeeClass, //testClassReference,324SUSPEND_NONE, "ExceptionRequest1");325eventRequest1.enable();326break;327328case 1:329eventRequest2 = settingExceptionRequest (mainThread,330debuggeeClass, //testClassReference,331SUSPEND_THREAD, "ExceptionRequest2");332eventRequest2.enable();333break;334335case 2:336eventRequest3 = settingExceptionRequest (mainThread,337debuggeeClass, //testClassReference,338SUSPEND_ALL, "ExceptionRequest3");339eventRequest3.enable();340break;341342case 3:343eventRequest1 = settingExceptionRequest (mainThread,344debuggeeClass, // testClassReference,345SUSPEND_NONE, "ExceptionRequest4");346eventRequest1.enable();347eventRequest2 = settingExceptionRequest (mainThread,348debuggeeClass, //testClassReference,349SUSPEND_THREAD, "ExceptionRequest5");350eventRequest2.enable();351break;352353case 4:354eventRequest1 = settingExceptionRequest (mainThread,355debuggeeClass, //testClassReference,356SUSPEND_NONE, "ExceptionRequest6");357eventRequest1.enable();358eventRequest3 = settingExceptionRequest (mainThread,359debuggeeClass, //testClassReference,360SUSPEND_ALL, "ExceptionRequest7");361eventRequest3.enable();362break;363364case 5:365eventRequest2 = settingExceptionRequest (mainThread,366debuggeeClass, //testClassReference,367SUSPEND_THREAD, "ExceptionRequest8");368eventRequest2.enable();369eventRequest3 = settingExceptionRequest (mainThread,370debuggeeClass, //testClassReference,371SUSPEND_ALL, "ExceptionRequest9");372eventRequest3.enable();373break;374375case 6:376eventRequest1 = settingExceptionRequest (mainThread,377debuggeeClass, //testClassReference,378SUSPEND_NONE, "ExceptionRequest10");379eventRequest1.enable();380eventRequest2 = settingExceptionRequest (mainThread,381debuggeeClass, //testClassReference,382SUSPEND_THREAD, "ExceptionRequest11");383eventRequest2.enable();384eventRequest3 = settingExceptionRequest (mainThread,385debuggeeClass, //testClassReference,386SUSPEND_ALL, "ExceptionRequest12");387eventRequest3.enable();388break;389390391default:392throw new JDITestRuntimeException("** default case 2 **");393}394395log2("......waiting for new ExceptionEvent : " + i);396mainThread.resume();397getEventSet();398399Event newEvent = eventIterator.nextEvent();400log2(" got new Event with propety 'number' == " + newEvent.request().getProperty("number"));401if ( !( newEvent instanceof ExceptionEvent)) {402log3("ERROR: new event is not ExceptionEvent");403testExitCode = FAILED;404} else {405log2(" Event is instanceof ExceptionEvent");406policy = eventSet.suspendPolicy();407if (policy != policyExpected[i]) {408log3("ERROR: eventSet.suspendPolicy() != policyExpected");409log3(" eventSet.suspendPolicy() == " + policy);410log3(" policyExpected == " + policyExpected[i]);411testExitCode = FAILED;412}413}414415switch (i) {416case 0: eventRequest1.disable(); break;417case 1: eventRequest2.disable(); break;418case 2: eventRequest3.disable(); break;419case 3: eventRequest1.disable(); eventRequest2.disable(); break;420case 4: eventRequest1.disable(); eventRequest3.disable(); break;421case 5: eventRequest2.disable(); eventRequest3.disable(); break;422case 6: eventRequest1.disable(); eventRequest2.disable();423eventRequest3.disable(); break;424}425426switch (policy) {427case SUSPEND_NONE : break;428case SUSPEND_THREAD : mainThread.resume(); break;429case SUSPEND_ALL : vm.resume(); break;430default: throw new JDITestRuntimeException("** default case 3 **");431}432433//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~434}435log1(" TESTING ENDS");436return;437}438439// ============================== test's additional methods440441private ExceptionRequest settingExceptionRequest ( ThreadReference thread,442ReferenceType testedClass,443int suspendPolicy,444String property )445throws JDITestRuntimeException {446try {447log2("......setting up ExceptionRequest:");448log2(" thread: " + thread + "; class: " + testedClass + "; property: " + property);449450ExceptionRequest451excr = eventRManager.createExceptionRequest(null, true, true);452excr.putProperty("number", property);453excr.addThreadFilter(thread);454excr.addClassFilter(testedClass);455excr.setSuspendPolicy(suspendPolicy);456457log2(" ExceptionRequest has been set up");458return excr;459} catch ( Exception e ) {460log3("ERROR: ATTENTION: Exception within settingExceptionRequest() : " + e);461log3(" ExceptionRequest HAS NOT BEEN SET UP");462throw new JDITestRuntimeException("** FAILURE to set up ExceptionRequest **");463}464}465466}467468469