Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequest/suspendPolicy/suspendpolicy001.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.EventRequest.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* EventRequest. <BR>39* <BR>40* The test checks that results of the method <BR>41* <code>com.sun.jdi.EventRequest.suspendPolicy()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks up on following assertions: <BR>45* - Returns: the current suspend mode for this request <BR>46* The cases to check include Requests of all sub-types. <BR>47* <BR>48* The test has three phases and works as follows. <BR>49* <BR>50* In first phase, <BR>51* upon launching debuggee's VM which will be suspended, <BR>52* a debugger waits for the VMStartEvent within a predefined <BR>53* time interval. If no the VMStartEvent received, the test is FAILED. <BR>54* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>55* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>56* and waits for the event within the predefined time interval. <BR>57* If no the ClassPrepareEvent received, the test is FAILED. <BR>58* Upon getting the ClassPrepareEvent, <BR>59* the debugger sets up the breakpoint with SUSPEND_ALL <BR>60* within debuggee's special methodForCommunication(). <BR>61* <BR>62* In second phase to check the assertion, <BR>63* the debugger and the debuggee perform the following loop. <BR>64* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>65* - The debuggee prepares new check case (as if) and invokes <BR>66* the methodForCommunication to be suspended and <BR>67* to inform the debugger with the event. <BR>68* - Upon getting the BreakpointEvent, <BR>69* the debugger performs the check case required. <BR>70* <BR>71* In third phase, at the end of the test, the debuggee changes <BR>72* the value of the "instruction" which the debugger and debuggee <BR>73* use to inform each other of needed actions, and both end. <BR>74* <BR>75*/7677public class suspendpolicy001 extends JDIBase {7879public static void main (String argv[]) {8081int result = run(argv, System.out);8283System.exit(result + PASS_BASE);84}8586public static int run (String argv[], PrintStream out) {8788int exitCode = new suspendpolicy001().runThis(argv, out);8990if (exitCode != PASSED) {91System.out.println("TEST FAILED");92}93return testExitCode;94}9596// ************************************************ test parameters9798private String debuggeeName =99"nsk.jdi.EventRequest.suspendPolicy.suspendpolicy001a";100101private String testedClassName =102"nsk.jdi.EventRequest.suspendPolicy.TestClass11";103104105//====================================================== test program106107private int runThis (String argv[], PrintStream out) {108109argsHandler = new ArgumentHandler(argv);110logHandler = new Log(out, argsHandler);111Binder binder = new Binder(argsHandler, logHandler);112113waitTime = argsHandler.getWaitTime() * 60000;114115try {116log2("launching a debuggee :");117log2(" " + debuggeeName);118if (argsHandler.verbose()) {119debuggee = binder.bindToDebugee(debuggeeName + " -vbs");120} else {121debuggee = binder.bindToDebugee(debuggeeName);122}123if (debuggee == null) {124log3("ERROR: no debuggee launched");125return FAILED;126}127log2("debuggee launched");128} catch ( Exception e ) {129log3("ERROR: Exception : " + e);130log2(" test cancelled");131return FAILED;132}133134debuggee.redirectOutput(logHandler);135136vm = debuggee.VM();137138eventQueue = vm.eventQueue();139if (eventQueue == null) {140log3("ERROR: eventQueue == null : TEST ABORTED");141vm.exit(PASS_BASE);142return FAILED;143}144145log2("invocation of the method runTest()");146switch (runTest()) {147148case 0 : log2("test phase has finished normally");149log2(" waiting for the debuggee to finish ...");150debuggee.waitFor();151152log2("......getting the debuggee's exit status");153int status = debuggee.getStatus();154if (status != PASS_BASE) {155log3("ERROR: debuggee returned UNEXPECTED exit status: " +156status + " != PASS_BASE");157testExitCode = FAILED;158} else {159log2("......debuggee returned expected exit status: " +160status + " == PASS_BASE");161}162break;163164default : log3("ERROR: runTest() returned unexpected value");165166case 1 : log3("test phase has not finished normally: debuggee is still alive");167log2("......forcing: vm.exit();");168testExitCode = FAILED;169try {170vm.exit(PASS_BASE);171} catch ( Exception e ) {172log3("ERROR: Exception : " + e);173}174break;175176case 2 : log3("test cancelled due to VMDisconnectedException");177log2("......trying: vm.process().destroy();");178testExitCode = FAILED;179try {180Process vmProcess = vm.process();181if (vmProcess != null) {182vmProcess.destroy();183}184} catch ( Exception e ) {185log3("ERROR: Exception : " + e);186}187break;188}189190return testExitCode;191}192193194/*195* Return value: 0 - normal end of the test196* 1 - ubnormal end of the test197* 2 - VMDisconnectedException while test phase198*/199200private int runTest() {201202try {203testRun();204205log2("waiting for VMDeathEvent");206getEventSet();207if (eventIterator.nextEvent() instanceof VMDeathEvent)208return 0;209210log3("ERROR: last event is not the VMDeathEvent");211return 1;212} catch ( VMDisconnectedException e ) {213log3("ERROR: VMDisconnectedException : " + e);214return 2;215} catch ( Exception e ) {216log3("ERROR: Exception : " + e);217return 1;218}219220}221222private void testRun()223throws JDITestRuntimeException, Exception {224225eventRManager = vm.eventRequestManager();226227ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();228cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);229cpRequest.addClassFilter(debuggeeName);230231cpRequest.enable();232vm.resume();233getEventSet();234cpRequest.disable();235236ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();237debuggeeClass = event.referenceType();238239if (!debuggeeClass.name().equals(debuggeeName))240throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");241242log2(" received: ClassPrepareEvent for debuggeeClass");243244String bPointMethod = "methodForCommunication";245String lineForComm = "lineForComm";246247ThreadReference mainThread = debuggee.threadByNameOrThrow("main");248249BreakpointRequest bpRequest = settingBreakpoint(mainThread,250debuggeeClass,251bPointMethod, lineForComm, "zero");252bpRequest.enable();253254//------------------------------------------------------ testing section255256257EventRequest eventRequest1 = null;258259String fieldName1 = "var101";260String fieldName2 = "excObj";261262ThreadReference thread1 = null;263String threadName1 = "thread1";264265ReferenceType testClassReference = null;266267String propertyName = "name";268String propertyValue1 = "value1";269String propertyValue2 = "value2";270271272log1(" TESTING BEGINS");273274for (int i = 0; ; i++) {275276vm.resume();277breakpointForCommunication();278279int instruction = ((IntegerValue)280(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();281282if (instruction == 0) {283vm.resume();284break;285}286287log1(":::::: case: # " + i);288289//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part290291292switch (i) {293294case 0:295thread1 = debuggee.threadByNameOrThrow(threadName1);296297log2("......setting up StepRequest");298eventRequest1 = eventRManager.createStepRequest299(thread1, StepRequest.STEP_MIN, StepRequest.STEP_INTO);300break;301302case 1:303testClassReference = (ReferenceType) vm.classesByName(testedClassName).get(0);304305log2("......setting up AccessWatchpointRequest");306eventRequest1 = eventRManager.createAccessWatchpointRequest307(testClassReference.fieldByName(fieldName1));308break;309310case 2:311log2(".....setting up ModificationWatchpointRequest");312eventRequest1 = eventRManager.createModificationWatchpointRequest313(testClassReference.fieldByName(fieldName1));314break;315316case 3:317log2(".....setting up ClassPrepareRequest");318eventRequest1 = eventRManager.createClassPrepareRequest();319break;320321case 4:322log2(".....setting up ClassUnloadRequest");323eventRequest1 = eventRManager.createClassUnloadRequest();324break;325326case 5:327log2(".....setting up MethodEntryRequest");328eventRequest1 = eventRManager.createMethodEntryRequest();329break;330331case 6:332log2(".....setting up MethodExitRequest");333eventRequest1 = eventRManager.createMethodExitRequest();334break;335336case 7:337log2(".....setting up ThreadDeathRequest");338eventRequest1 = eventRManager.createThreadDeathRequest();339break;340341case 8:342log2(".....setting up ThreadStartRequest");343eventRequest1 = eventRManager.createThreadStartRequest();344break;345346case 9:347log2(".....setting up VMDeathRequest");348eventRequest1 = eventRManager.createVMDeathRequest();349break;350351case 10:352log2(".....setting up ExceptionRequest");353eventRequest1 = eventRManager.createExceptionRequest(354(ReferenceType)355(debuggeeClass.getValue(debuggeeClass.fieldByName(fieldName2))).type(),356true, true );357break;358359case 11:360log2(".....setting up BreakpointRequest");361eventRequest1 = eventRManager.createBreakpointRequest(breakpLocation);362break;363364365default:366throw new JDITestRuntimeException("** default case 2 **");367}368369log2("......eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_ALL);");370eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_ALL);371372log2(" checking up on eventRequest1");373if ( eventRequest1.suspendPolicy() != EventRequest.SUSPEND_ALL) {374testExitCode = FAILED;375log3("ERROR: suspendPolicy() != EventRequest.SUSPEND_ALL");376}377378log2("......eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD );");379eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD );380381log2(" checking up on eventRequest1");382if ( eventRequest1.suspendPolicy() != EventRequest.SUSPEND_EVENT_THREAD ) {383testExitCode = FAILED;384log3("ERROR: suspendPolicy() != EventRequest.SUSPEND_EVENT_THREAD ");385}386387log2("......eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_NONE);");388eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_NONE);389390log2(" checking up on eventRequest1");391if ( eventRequest1.suspendPolicy() != EventRequest.SUSPEND_NONE) {392testExitCode = FAILED;393log3("ERROR: suspendPolicy() != EventRequest.SUSPEND_NONE");394}395396eventRManager.deleteEventRequest(eventRequest1);397398//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~399}400log1(" TESTING ENDS");401return;402}403404}405406407