Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequest/setEnabled/setenabled003.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.setEnabled;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.setEnabled()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks up on the following assertion: <BR>45* - IllegalThreadStateException - <BR>46* if this is a StepRequest, val is true, and <BR>47* the thread named in the request has died. <BR>48* The cases to test include three states of <BR>49* the thread named in the request: not started, running, dead. <BR>50* <BR>51* The test has three phases and works as follows. <BR>52* <BR>53* In first phase, <BR>54* upon launching debuggee's VM which will be suspended, <BR>55* a debugger waits for the VMStartEvent within a predefined <BR>56* time interval. If no the VMStartEvent received, the test is FAILED. <BR>57* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>58* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>59* and waits for the event within the predefined time interval. <BR>60* If no the ClassPrepareEvent received, the test is FAILED. <BR>61* Upon getting the ClassPrepareEvent, <BR>62* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>63* within debuggee's special methodForCommunication(). <BR>64* <BR>65* In second phase to check the assertion, <BR>66* the debugger and the debuggee perform the following. <BR>67* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>68* - The debuggee prepares new check case and invokes <BR>69* the methodForCommunication to be suspended and <BR>70* to inform the debugger with the event. <BR>71* - Upon getting the BreakpointEvent, <BR>72* the debugger performs the check case required. <BR>73* <BR>74* In third phase, at the end of the test, the debuggee changes <BR>75* the value of the "instruction" which the debugger and debuggee <BR>76* use to inform each other of needed actions, and both end. <BR>77* <BR>78*/7980public class setenabled003 extends JDIBase {8182public static void main (String argv[]) {8384int result = run(argv, System.out);8586System.exit(result + PASS_BASE);87}8889public static int run (String argv[], PrintStream out) {9091int exitCode = new setenabled003().runThis(argv, out);9293if (exitCode != PASSED) {94System.out.println("TEST FAILED");95}96return testExitCode;97}9899// ************************************************ test parameters100101private String debuggeeName =102"nsk.jdi.EventRequest.setEnabled.setenabled003a";103104private String testedClassName =105"nsk.jdi.EventRequest.setEnabled.Thread1setenabled003a";106107//====================================================== 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;260261ThreadReference thread1 = null;262String threadName1 = "thread1";263264265log1(" TESTING BEGINS");266267for (int i = 0; ; i++) {268269vm.resume();270breakpointForCommunication();271272int instruction = ((IntegerValue)273(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();274275if (instruction == 0) {276vm.resume();277break;278}279280log1(":::::: case: # " + i);281282//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part283284vm.suspend();285switch (i) {286287case 0:288thread1 = (ThreadReference)289debuggeeClass.getValue(debuggeeClass.fieldByName(threadName1));290291log2("......setting up StepRequest");292eventRequest1 = eventRManager.createStepRequest293(thread1, StepRequest.STEP_MIN, StepRequest.STEP_INTO);294try {295log2("......eventRequest1.setEnabled(true); IllegalThreadStateException is expected");296eventRequest1.setEnabled(true);297log3("ERROR: case 0 : no IllegalThreadStateException");298testExitCode = FAILED;299} catch ( IllegalThreadStateException e ) {300log2(" not started thread");301log2(" : IllegalThreadStateException when setEnabled(true)");302}303try {304log2("......eventRequest1.setEnabled(false); IllegalThreadStateException is not expected");305eventRequest1.setEnabled(false);306log2(" no IllegalThreadStateException");307} catch ( IllegalThreadStateException e ) {308testExitCode = FAILED;309log3("ERROR: case 0 : not started thread");310log3(" : IllegalThreadStateException when setEnabled(false)");311}312break;313314case 1:315try {316log2("......eventRequest1.setEnabled(true); IllegalThreadStateException is not expected");317eventRequest1.setEnabled(true);318log2(" no IllegalThreadStateException");319} catch ( IllegalThreadStateException e ) {320testExitCode = FAILED;321log3("ERROR: case 1 : running thread");322log3(" : IllegalThreadStateException when setEnabled(true)");323}324try {325log2("......eventRequest1.setEnabled(false); IllegalThreadStateException is not expected");326eventRequest1.setEnabled(false);327log2(" no IllegalThreadStateException");328} catch ( IllegalThreadStateException e ) {329testExitCode = FAILED;330log3("ERROR: case 1 : running thread");331log3(" : IllegalThreadStateException when setEnabled(false)");332}333break;334335case 2:336try {337log2("......eventRequest1.setEnabled(true); IllegalThreadStateException is expected");338eventRequest1.setEnabled(true);339testExitCode = FAILED;340log3("ERROR: case 2 : dead thread");341log3(" : NO IllegalThreadStateException when setEnabled(true)");342} catch ( IllegalThreadStateException e ) {343log2(" IllegalThreadStateException");344}345try {346log2("......eventRequest1.setEnabled(false); IllegalThreadStateException is not expected");347eventRequest1.setEnabled(false);348log2(" no IllegalThreadStateException");349} catch ( IllegalThreadStateException e ) {350testExitCode = FAILED;351log3("ERROR: case 2 : dead thread");352log3(" : IllegalThreadStateException when setEnabled(false)");353}354break;355356357default:358throw new JDITestRuntimeException("** default case 2 **");359}360vm.resume();361362//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~363}364log1(" TESTING ENDS");365return;366}367368}369370371