Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequest/isEnabled/isenabled001.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.isEnabled;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.isEnabled()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks up on if a value returned by the method <BR>45* EventRequest.isEnabled() matchs a value set up with <BR>46* the method EventRequest.setEnabled(). <BR>47* The cases to test include all sub-types of EventRequest. <BR>48* <BR>49* The test has three phases and works as follows. <BR>50* <BR>51* In first phase, <BR>52* upon launching debuggee's VM which will be suspended, <BR>53* a debugger waits for the VMStartEvent within a predefined <BR>54* time interval. If no the VMStartEvent received, the test is FAILED. <BR>55* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>56* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>57* and waits for the event within the predefined time interval. <BR>58* If no the ClassPrepareEvent received, the test is FAILED. <BR>59* Upon getting the ClassPrepareEvent, <BR>60* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>61* within debuggee's special methodForCommunication(). <BR>62* <BR>63* In second phase to check the assertion, <BR>64* the debugger and the debuggee perform the following loop. <BR>65* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>66* - The debuggee prepares new check case (as if) and invokes <BR>67* the methodForCommunication to be suspended and <BR>68* to inform the debugger with the event. <BR>69* - Upon getting the BreakpointEvent, <BR>70* the debugger performs the check case required. <BR>71* <BR>72* In third phase, at the end of the test, the debuggee changes <BR>73* the value of the "instruction" which the debugger and debuggee <BR>74* use to inform each other of needed actions, and both end. <BR>75* <BR>76*/7778public class isenabled001 extends JDIBase {7980public static void main (String argv[]) {8182int result = run(argv, System.out);8384System.exit(result + PASS_BASE);85}8687public static int run (String argv[], PrintStream out) {8889int exitCode = new isenabled001().runThis(argv, out);9091if (exitCode != PASSED) {92System.out.println("TEST FAILED");93}94return testExitCode;95}9697// ************************************************ test parameters9899private String debuggeeName =100"nsk.jdi.EventRequest.isEnabled.isenabled001a";101102private String testedClassName =103"nsk.jdi.EventRequest.isEnabled.TestClass11";104105//====================================================== 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;114115116try {117log2("launching a debuggee :");118log2(" " + debuggeeName);119if (argsHandler.verbose()) {120debuggee = binder.bindToDebugee(debuggeeName + " -vbs");121} else {122debuggee = binder.bindToDebugee(debuggeeName);123}124if (debuggee == null) {125log3("ERROR: no debuggee launched");126return FAILED;127}128log2("debuggee launched");129} catch ( Exception e ) {130log3("ERROR: Exception : " + e);131log2(" test cancelled");132return FAILED;133}134135debuggee.redirectOutput(logHandler);136137vm = debuggee.VM();138139eventQueue = vm.eventQueue();140if (eventQueue == null) {141log3("ERROR: eventQueue == null : TEST ABORTED");142vm.exit(PASS_BASE);143return FAILED;144}145146log2("invocation of the method runTest()");147switch (runTest()) {148149case 0 : log2("test phase has finished normally");150log2(" waiting for the debuggee to finish ...");151debuggee.waitFor();152153log2("......getting the debuggee's exit status");154int status = debuggee.getStatus();155if (status != PASS_BASE) {156log3("ERROR: debuggee returned UNEXPECTED exit status: " +157status + " != PASS_BASE");158testExitCode = FAILED;159} else {160log2("......debuggee returned expected exit status: " +161status + " == PASS_BASE");162}163break;164165default : log3("ERROR: runTest() returned unexpected value");166167case 1 : log3("test phase has not finished normally: debuggee is still alive");168log2("......forcing: vm.exit();");169testExitCode = FAILED;170try {171vm.exit(PASS_BASE);172} catch ( Exception e ) {173log3("ERROR: Exception : " + e);174}175break;176177case 2 : log3("test cancelled due to VMDisconnectedException");178log2("......trying: vm.process().destroy();");179testExitCode = FAILED;180try {181Process vmProcess = vm.process();182if (vmProcess != null) {183vmProcess.destroy();184}185} catch ( Exception e ) {186log3("ERROR: Exception : " + e);187}188break;189}190191return testExitCode;192}193194195/*196* Return value: 0 - normal end of the test197* 1 - ubnormal end of the test198* 2 - VMDisconnectedException while test phase199*/200201private int runTest() {202203try {204testRun();205206log2("waiting for VMDeathEvent");207getEventSet();208if (eventIterator.nextEvent() instanceof VMDeathEvent)209return 0;210211log3("ERROR: last event is not the VMDeathEvent");212return 1;213} catch ( VMDisconnectedException e ) {214log3("ERROR: VMDisconnectedException : " + e);215return 2;216} catch ( Exception e ) {217log3("ERROR: Exception : " + e);218return 1;219}220221}222223private void testRun()224throws JDITestRuntimeException, Exception {225226eventRManager = vm.eventRequestManager();227228ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();229cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);230cpRequest.addClassFilter(debuggeeName);231232cpRequest.enable();233vm.resume();234getEventSet();235cpRequest.disable();236237ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();238debuggeeClass = event.referenceType();239240if (!debuggeeClass.name().equals(debuggeeName))241throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");242243log2(" received: ClassPrepareEvent for debuggeeClass");244245String bPointMethod = "methodForCommunication";246String lineForComm = "lineForComm";247248ThreadReference mainThread = debuggee.threadByNameOrThrow("main");249250BreakpointRequest bpRequest = settingBreakpoint(mainThread,251debuggeeClass,252bPointMethod, lineForComm, "zero");253bpRequest.enable();254255//------------------------------------------------------ testing section256257258EventRequest eventRequest1 = null;259260String fieldName1 = "var101";261String fieldName2 = "excObj";262263ThreadReference thread1 = null;264String threadName1 = "thread1";265266ReferenceType testClassReference = null;267268269log1(" TESTING BEGINS");270271for (int i = 0; ; i++) {272273vm.resume();274breakpointForCommunication();275276int instruction = ((IntegerValue)277(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();278279if (instruction == 0) {280vm.resume();281break;282}283284log1(":::::: case: # " + i);285286//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part287288289switch (i) {290291case 0:292thread1 = debuggee.threadByNameOrThrow(threadName1);293294log2("......setting up StepRequest");295eventRequest1 = eventRManager.createStepRequest296(thread1, StepRequest.STEP_MIN, StepRequest.STEP_INTO);297break;298299case 1:300testClassReference = (ReferenceType) vm.classesByName(testedClassName).get(0);301302log2("......setting up AccessWatchpointRequest");303eventRequest1 = eventRManager.createAccessWatchpointRequest304(testClassReference.fieldByName(fieldName1));305break;306307case 2:308log2(".....setting up ModificationWatchpointRequest");309eventRequest1 = eventRManager.createModificationWatchpointRequest310(testClassReference.fieldByName(fieldName1));311break;312313case 3:314log2(".....setting up ClassPrepareRequest");315eventRequest1 = eventRManager.createClassPrepareRequest();316break;317318case 4:319log2(".....setting up ClassUnloadRequest");320eventRequest1 = eventRManager.createClassUnloadRequest();321break;322323case 5:324log2(".....setting up MethodEntryRequest");325eventRequest1 = eventRManager.createMethodEntryRequest();326break;327328case 6:329log2(".....setting up MethodExitRequest");330eventRequest1 = eventRManager.createMethodExitRequest();331break;332333case 7:334log2(".....setting up ThreadDeathRequest");335eventRequest1 = eventRManager.createThreadDeathRequest();336break;337338case 8:339log2(".....setting up ThreadStartRequest");340eventRequest1 = eventRManager.createThreadStartRequest();341break;342343case 9:344log2(".....setting up VMDeathRequest");345eventRequest1 = eventRManager.createVMDeathRequest();346break;347348case 10:349log2(".....setting up ExceptionRequest");350eventRequest1 = eventRManager.createExceptionRequest(351(ReferenceType)352(debuggeeClass.getValue(debuggeeClass.fieldByName(fieldName2))).type(),353true, true );354break;355356case 11:357log2(".....setting up BreakpointRequest");358eventRequest1 = eventRManager.createBreakpointRequest(breakpLocation);359break;360361362default:363throw new JDITestRuntimeException("** default case 2 **");364}365366vm.suspend();367log2("......eventRequest1.setEnabled(true);");368eventRequest1.setEnabled(true);369log2(" checking up on eventRequest1");370if ( !eventRequest1.isEnabled() ) {371testExitCode = FAILED;372log3("ERROR: EventRequest is not enabled");373}374375log2("......eventRequest1.setEnabled(false);");376eventRequest1.setEnabled(false);377log2(" checking up on eventRequest1");378if ( eventRequest1.isEnabled() ) {379testExitCode = FAILED;380log3("ERROR: EventRequest is enabled");381}382vm.resume();383384//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~385}386log1(" TESTING ENDS");387return;388}389390}391392393