Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequest/setEnabled/setenabled002.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* - InvalidRequestStateException - <BR>49* if this request has been deleted. <BR>50* The cases to test include all sub-types of EventRequest. <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 assertion, <BR>67* the debugger and the debuggee perform the following. <BR>68* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>69* - The debuggee prepares new check case and invokes <BR>70* the methodForCommunication to be suspended and <BR>71* to inform the debugger with the event. <BR>72* - Upon getting the BreakpointEvent, <BR>73* the debugger performs the check case required. <BR>74* <BR>75* In third phase, at the end of the test, the debuggee changes <BR>76* the value of the "instruction" which the debugger and debuggee <BR>77* use to inform each other of needed actions, and both end. <BR>78* <BR>79*/8081public class setenabled002 extends JDIBase {8283public static void main (String argv[]) {8485int result = run(argv, System.out);8687System.exit(result + PASS_BASE);88}8990public static int run (String argv[], PrintStream out) {9192int exitCode = new setenabled002().runThis(argv, out);9394if (exitCode != PASSED) {95System.out.println("TEST FAILED");96}97return testExitCode;98}99100// ************************************************ test parameters101102private String debuggeeName =103"nsk.jdi.EventRequest.setEnabled.setenabled002a";104105private String testedClassName =106"nsk.jdi.EventRequest.setEnabled.setenabled002aTestClass11";107108//====================================================== test program109110private int runThis (String argv[], PrintStream out) {111112argsHandler = new ArgumentHandler(argv);113logHandler = new Log(out, argsHandler);114Binder binder = new Binder(argsHandler, logHandler);115116waitTime = argsHandler.getWaitTime() * 60000;117118try {119log2("launching a debuggee :");120log2(" " + debuggeeName);121if (argsHandler.verbose()) {122debuggee = binder.bindToDebugee(debuggeeName + " -vbs");123} else {124debuggee = binder.bindToDebugee(debuggeeName);125}126if (debuggee == null) {127log3("ERROR: no debuggee launched");128return FAILED;129}130log2("debuggee launched");131} catch ( Exception e ) {132log3("ERROR: Exception : " + e);133log2(" test cancelled");134return FAILED;135}136137debuggee.redirectOutput(logHandler);138139vm = debuggee.VM();140141eventQueue = vm.eventQueue();142if (eventQueue == null) {143log3("ERROR: eventQueue == null : TEST ABORTED");144vm.exit(PASS_BASE);145return FAILED;146}147148log2("invocation of the method runTest()");149switch (runTest()) {150151case 0 : log2("test phase has finished normally");152log2(" waiting for the debuggee to finish ...");153debuggee.waitFor();154155log2("......getting the debuggee's exit status");156int status = debuggee.getStatus();157if (status != PASS_BASE) {158log3("ERROR: debuggee returned UNEXPECTED exit status: " +159status + " != PASS_BASE");160testExitCode = FAILED;161} else {162log2("......debuggee returned expected exit status: " +163status + " == PASS_BASE");164}165break;166167default : log3("ERROR: runTest() returned unexpected value");168169case 1 : log3("test phase has not finished normally: debuggee is still alive");170log2("......forcing: vm.exit();");171testExitCode = FAILED;172try {173vm.exit(PASS_BASE);174} catch ( Exception e ) {175log3("ERROR: Exception : " + e);176}177break;178179case 2 : log3("test cancelled due to VMDisconnectedException");180log2("......trying: vm.process().destroy();");181testExitCode = FAILED;182try {183Process vmProcess = vm.process();184if (vmProcess != null) {185vmProcess.destroy();186}187} catch ( Exception e ) {188log3("ERROR: Exception : " + e);189}190break;191}192193return testExitCode;194}195196197/*198* Return value: 0 - normal end of the test199* 1 - ubnormal end of the test200* 2 - VMDisconnectedException while test phase201*/202203private int runTest() {204205try {206testRun();207208log2("waiting for VMDeathEvent");209getEventSet();210if (eventIterator.nextEvent() instanceof VMDeathEvent)211return 0;212213log3("ERROR: last event is not the VMDeathEvent");214return 1;215} catch ( VMDisconnectedException e ) {216log3("ERROR: VMDisconnectedException : " + e);217return 2;218} catch ( Exception e ) {219log3("ERROR: Exception : " + e);220return 1;221}222223}224225private void testRun()226throws JDITestRuntimeException, Exception {227228eventRManager = vm.eventRequestManager();229230ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();231cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);232cpRequest.addClassFilter(debuggeeName);233234cpRequest.enable();235vm.resume();236getEventSet();237cpRequest.disable();238239ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();240debuggeeClass = event.referenceType();241242if (!debuggeeClass.name().equals(debuggeeName))243throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");244245log2(" received: ClassPrepareEvent for debuggeeClass");246247String bPointMethod = "methodForCommunication";248String lineForComm = "lineForComm";249250ThreadReference mainThread = debuggee.threadByNameOrThrow("main");251252BreakpointRequest bpRequest = settingBreakpoint(mainThread,253debuggeeClass,254bPointMethod, lineForComm, "zero");255bpRequest.enable();256257//------------------------------------------------------ testing section258259260EventRequest eventRequest1 = null;261262String fieldName1 = "var101";263String fieldName2 = "excObj";264265ThreadReference thread1 = null;266String threadName1 = "thread1";267268ReferenceType testClassReference = null;269270271log1(" TESTING BEGINS");272273for (int i = 0; ; i++) {274275vm.resume();276breakpointForCommunication();277278int instruction = ((IntegerValue)279(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();280281if (instruction == 0) {282vm.resume();283break;284}285286log1(":::::: case: # " + i);287288//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part289290291switch (i) {292293case 0:294thread1 = debuggee.threadByNameOrThrow(threadName1);295296log2("......setting up StepRequest");297eventRequest1 = eventRManager.createStepRequest298(thread1, StepRequest.STEP_MIN, StepRequest.STEP_INTO);299vm.resume();300breakpointForCommunication();301break;302303case 1:304testClassReference = (ReferenceType) vm.classesByName(testedClassName).get(0);305306log2("......setting up AccessWatchpointRequest");307eventRequest1 = eventRManager.createAccessWatchpointRequest308(testClassReference.fieldByName(fieldName1));309break;310311case 2:312log2(".....setting up ModificationWatchpointRequest");313eventRequest1 = eventRManager.createModificationWatchpointRequest314(testClassReference.fieldByName(fieldName1));315break;316317case 3:318log2(".....setting up ClassPrepareRequest");319eventRequest1 = eventRManager.createClassPrepareRequest();320break;321322case 4:323log2(".....setting up ClassUnloadRequest");324eventRequest1 = eventRManager.createClassUnloadRequest();325break;326327case 5:328log2(".....setting up MethodEntryRequest");329eventRequest1 = eventRManager.createMethodEntryRequest();330break;331332case 6:333log2(".....setting up MethodExitRequest");334eventRequest1 = eventRManager.createMethodExitRequest();335break;336337case 7:338log2(".....setting up ThreadDeathRequest");339eventRequest1 = eventRManager.createThreadDeathRequest();340break;341342case 8:343log2(".....setting up ThreadStartRequest");344eventRequest1 = eventRManager.createThreadStartRequest();345break;346347case 9:348log2(".....setting up VMDeathRequest");349eventRequest1 = eventRManager.createVMDeathRequest();350break;351352case 10:353log2(".....setting up ExceptionRequest");354eventRequest1 = eventRManager.createExceptionRequest(355(ReferenceType)356(debuggeeClass.getValue(debuggeeClass.fieldByName(fieldName2))).type(),357true, true );358break;359360case 11:361log2(".....setting up BreakpointRequest");362eventRequest1 = eventRManager.createBreakpointRequest(breakpLocation);363break;364365366default:367throw new JDITestRuntimeException("** default case 2 **");368}369370vm.suspend();371if (eventRequest1 instanceof StepRequest) {372try {373log2("......eventRequest1.setEnabled(true); IllegalThreadStateException is expected");374eventRequest1.setEnabled(true);375testExitCode = FAILED;376log3("ERROR: NO IllegalThreadStateException for StepRequest");377} catch ( IllegalThreadStateException e ) {378log2(" IllegalThreadStateException");379}380try {381log2("......eventRequest1.setEnabled(false); IllegalThreadStateException is not expected");382eventRequest1.setEnabled(false);383log2(" no IllegalThreadStateException for StepRequest");384} catch ( IllegalThreadStateException e ) {385testExitCode = FAILED;386log3("ERROR: IllegalThreadStateException");387}388}389390log2("......eventRManager.deleteEventRequest(eventRequest1);");391eventRManager.deleteEventRequest(eventRequest1);392try {393log2("......eventRequest1.setEnabled(true); InvalidRequestStateException is expected");394eventRequest1.setEnabled(true);395testExitCode = FAILED;396log3("ERROR: NO InvalidRequestStateException");397} catch ( InvalidRequestStateException e ) {398log2(" InvalidRequestStateException");399}400try {401log2("......eventRequest1.setEnabled(false); InvalidRequestStateException is expected");402eventRequest1.setEnabled(false);403testExitCode = FAILED;404log3("ERROR: NO InvalidRequestStateException");405} catch ( InvalidRequestStateException e ) {406log2(" InvalidRequestStateException");407}408vm.resume();409410//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~411}412log1(" TESTING ENDS");413return;414}415416}417418419