Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequest/enable/enable002.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.enable;2425import nsk.share.*;26import nsk.share.jdi.*;2728import com.sun.jdi.*;29import com.sun.jdi.event.*;30import com.sun.jdi.request.*;3132import java.io.*;3334/**35* The test for the implementation of an object of the type <BR>36* EventRequest. <BR>37* <BR>38* The test checks that results of the method <BR>39* <code>com.sun.jdi.EventRequest.enable()</code> <BR>40* complies with its spec. <BR>41* <BR>42* The test checks up on the following assertion: <BR>43* Throws: InvalidRequestStateException - <BR>44* if this request has been deleted. <BR>45* IllegalThreadStateException - <BR>46* if this is a StepRequest and <BR>47* the thread named in the request has died. <BR>48* The cases to test include all sub-types of EventRequest. <BR>49* <BR>50* The test has three phases and works as follows. <BR>51* <BR>52* In first phase, <BR>53* upon launching debuggee's VM which will be suspended, <BR>54* a debugger waits for the VMStartEvent within a predefined <BR>55* time interval. If no the VMStartEvent received, the test is FAILED. <BR>56* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>57* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>58* and waits for the event within the predefined time interval. <BR>59* If no the ClassPrepareEvent received, the test is FAILED. <BR>60* Upon getting the ClassPrepareEvent, <BR>61* the debugger sets up the breakpoint with SUSPEND_ALL <BR>62* within debuggee's special methodForCommunication(). <BR>63* <BR>64* In second phase to check the assertion, <BR>65* the debugger and the debuggee perform the following loop. <BR>66* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>67* - The debuggee prepares new check case (as if) and invokes <BR>68* the methodForCommunication to be suspended and <BR>69* to inform the debugger with the event. <BR>70* - Upon getting the BreakpointEvent, <BR>71* the debugger performs the check case required. <BR>72* <BR>73* In third phase, at the end of the test, the debuggee changes <BR>74* the value of the "instruction" which the debugger and debuggee <BR>75* use to inform each other of needed actions, and both end. <BR>76* <BR>77*/7879public class enable002 extends JDIBase {8081public static void main (String argv[]) {8283int result = run(argv, System.out);8485System.exit(result + PASS_BASE);86}8788public static int run (String argv[], PrintStream out) {8990int exitCode = new enable002().runThis(argv, out);9192if (exitCode != PASSED) {93System.out.println("TEST FAILED");94}95return testExitCode;96}9798// ************************************************ test parameters99100private String debuggeeName =101"nsk.jdi.EventRequest.enable.enable002a";102103private String testedClassName =104"nsk.jdi.EventRequest.enable.enable002aTestClass11";105106107//====================================================== test program108109private int runThis (String argv[], PrintStream out) {110111argsHandler = new ArgumentHandler(argv);112logHandler = new Log(out, argsHandler);113Binder binder = new Binder(argsHandler, logHandler);114115waitTime = argsHandler.getWaitTime() * 60000;116117try {118log2("launching a debuggee :");119log2(" " + debuggeeName);120if (argsHandler.verbose()) {121debuggee = binder.bindToDebugee(debuggeeName + " -vbs");122} else {123debuggee = binder.bindToDebugee(debuggeeName);124}125if (debuggee == null) {126log3("ERROR: no debuggee launched");127return FAILED;128}129log2("debuggee launched");130} catch ( Exception e ) {131log3("ERROR: Exception : " + e);132log2(" test cancelled");133return FAILED;134}135136debuggee.redirectOutput(logHandler);137138vm = debuggee.VM();139140eventQueue = vm.eventQueue();141if (eventQueue == null) {142log3("ERROR: eventQueue == null : TEST ABORTED");143vm.exit(PASS_BASE);144return FAILED;145}146147log2("invocation of the method runTest()");148switch (runTest()) {149150case 0 : log2("test phase has finished normally");151log2(" waiting for the debuggee to finish ...");152debuggee.waitFor();153154log2("......getting the debuggee's exit status");155int status = debuggee.getStatus();156if (status != PASS_BASE) {157log3("ERROR: debuggee returned UNEXPECTED exit status: " +158status + " != PASS_BASE");159testExitCode = FAILED;160} else {161log2("......debuggee returned expected exit status: " +162status + " == PASS_BASE");163}164break;165166default : log3("ERROR: runTest() returned unexpected value");167168case 1 : log3("test phase has not finished normally: debuggee is still alive");169log2("......forcing: vm.exit();");170testExitCode = FAILED;171try {172vm.exit(PASS_BASE);173} catch ( Exception e ) {174log3("ERROR: Exception : " + e);175}176break;177178case 2 : log3("test cancelled due to VMDisconnectedException");179log2("......trying: vm.process().destroy();");180testExitCode = FAILED;181try {182Process vmProcess = vm.process();183if (vmProcess != null) {184vmProcess.destroy();185}186} catch ( Exception e ) {187log3("ERROR: Exception : " + e);188}189break;190}191192return testExitCode;193}194195196/*197* Return value: 0 - normal end of the test198* 1 - ubnormal end of the test199* 2 - VMDisconnectedException while test phase200*/201202private int runTest() {203204try {205testRun();206207log2("waiting for VMDeathEvent");208getEventSet();209if (eventIterator.nextEvent() instanceof VMDeathEvent)210return 0;211212log3("ERROR: last event is not the VMDeathEvent");213return 1;214} catch ( VMDisconnectedException e ) {215log3("ERROR: VMDisconnectedException : " + e);216return 2;217} catch ( Exception e ) {218log3("ERROR: Exception : " + e);219return 1;220}221222}223224private void testRun()225throws JDITestRuntimeException, Exception {226227eventRManager = vm.eventRequestManager();228229ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();230cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);231cpRequest.addClassFilter(debuggeeName);232233cpRequest.enable();234vm.resume();235getEventSet();236cpRequest.disable();237238ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();239debuggeeClass = event.referenceType();240241if (!debuggeeClass.name().equals(debuggeeName))242throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");243244log2(" received: ClassPrepareEvent for debuggeeClass");245246String bPointMethod = "methodForCommunication";247String lineForComm = "lineForComm";248249ThreadReference mainThread = debuggee.threadByNameOrThrow("main");250251BreakpointRequest bpRequest = settingBreakpoint(mainThread,252debuggeeClass,253bPointMethod, lineForComm, "zero");254bpRequest.enable();255256//------------------------------------------------------ testing section257258259EventRequest eventRequest1 = null;260261String fieldName1 = "var101";262String fieldName2 = "excObj";263264ThreadReference thread1 = null;265String threadName1 = "thread1";266267ReferenceType testClassReference = null;268269270log1(" TESTING BEGINS");271272for (int i = 0; ; i++) {273274vm.resume();275breakpointForCommunication();276277int instruction = ((IntegerValue)278(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();279280if (instruction == 0) {281vm.resume();282break;283}284285log1(":::::: case: # " + i);286287//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part288289290switch (i) {291292case 0:293thread1 = debuggee.threadByNameOrThrow(threadName1);294295log2("......setting up StepRequest");296eventRequest1 = eventRManager.createStepRequest297(thread1, StepRequest.STEP_MIN, StepRequest.STEP_INTO);298vm.resume();299breakpointForCommunication();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}368369if (eventRequest1 instanceof StepRequest) {370try {371log2("......eventRequest1.enable(); IllegalThreadStateException is expected");372eventRequest1.enable();373testExitCode = FAILED;374log3("ERROR: NO IllegalThreadStateException for StepRequest");375} catch ( IllegalThreadStateException e ) {376log2(" IllegalThreadStateException");377}378}379380log2("......eventRManager.deleteEventRequest(eventRequest1);");381eventRManager.deleteEventRequest(eventRequest1);382try {383log2("......eventRequest1.enable(); InvalidRequestStateException is expected");384eventRequest1.enable();385testExitCode = FAILED;386log3("ERROR: NO InvalidRequestStateException");387} catch ( InvalidRequestStateException e ) {388log2(" InvalidRequestStateException");389}390391//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~392}393log1(" TESTING ENDS");394return;395}396397}398399400