Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequest/disable/disable001.java
41161 views
/*1* Copyright (c) 2001, 2018, 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.disable;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.disable()</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.disable(). <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_ALL <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 disable001 {7980//----------------------------------------------------- templete section81static final int PASSED = 0;82static final int FAILED = 2;83static final int PASS_BASE = 95;8485//----------------------------------------------------- templete parameters86static final String87sHeader1 = "\n==> nsk/jdi/EventRequest/disable/disable001 ",88sHeader2 = "--> debugger: ",89sHeader3 = "##> debugger: ";9091//----------------------------------------------------- main method9293public static void main (String argv[]) {9495int result = run(argv, System.out);9697System.exit(result + PASS_BASE);98}99100public static int run (String argv[], PrintStream out) {101102int exitCode = new disable001().runThis(argv, out);103104if (exitCode != PASSED) {105System.out.println("TEST FAILED");106}107return testExitCode;108}109110//-------------------------------------------------- log procedures111112private static Log logHandler;113114private static void log1(String message) {115logHandler.display(sHeader1 + message);116}117private static void log2(String message) {118logHandler.display(sHeader2 + message);119}120private static void log3(String message) {121logHandler.complain(sHeader3 + message);122}123124// ************************************************ test parameters125126private String debuggeeName =127"nsk.jdi.EventRequest.disable.disable001a";128129private String testedClassName =130"nsk.jdi.EventRequest.disable.disable001aTestClass11";131132Location location = null; // !!!!!!!!!!!!! see settingBreakpoint133134//====================================================== test program135//------------------------------------------------------ common section136137static Debugee debuggee;138static ArgumentHandler argsHandler;139140static int waitTime;141142static VirtualMachine vm = null;143static EventRequestManager eventRManager = null;144static EventQueue eventQueue = null;145static EventSet eventSet = null;146static EventIterator eventIterator = null;147148static ReferenceType debuggeeClass = null;149150static int testExitCode = PASSED;151152153//------------------------------------------------------ methods154155private int runThis (String argv[], PrintStream out) {156157argsHandler = new ArgumentHandler(argv);158logHandler = new Log(out, argsHandler);159Binder binder = new Binder(argsHandler, logHandler);160161waitTime = argsHandler.getWaitTime() * 60000;162163try {164log2("launching a debuggee :");165log2(" " + debuggeeName);166if (argsHandler.verbose()) {167debuggee = binder.bindToDebugee(debuggeeName + " -vbs");168} else {169debuggee = binder.bindToDebugee(debuggeeName);170}171if (debuggee == null) {172log3("ERROR: no debuggee launched");173return FAILED;174}175log2("debuggee launched");176} catch ( Exception e ) {177log3("ERROR: Exception : " + e);178log2(" test cancelled");179return FAILED;180}181182debuggee.redirectOutput(logHandler);183184vm = debuggee.VM();185186eventQueue = vm.eventQueue();187if (eventQueue == null) {188log3("ERROR: eventQueue == null : TEST ABORTED");189vm.exit(PASS_BASE);190return FAILED;191}192193log2("invocation of the method runTest()");194switch (runTest()) {195196case 0 : log2("test phase has finished normally");197log2(" waiting for the debuggee to finish ...");198debuggee.waitFor();199200log2("......getting the debuggee's exit status");201int status = debuggee.getStatus();202if (status != PASS_BASE) {203log3("ERROR: debuggee returned UNEXPECTED exit status: " +204status + " != PASS_BASE");205testExitCode = FAILED;206} else {207log2("......debuggee returned expected exit status: " +208status + " == PASS_BASE");209}210break;211212default : log3("ERROR: runTest() returned unexpected value");213214case 1 : log3("test phase has not finished normally: debuggee is still alive");215log2("......forcing: vm.exit();");216testExitCode = FAILED;217try {218vm.exit(PASS_BASE);219} catch ( Exception e ) {220log3("ERROR: Exception : " + e);221}222break;223224case 2 : log3("test cancelled due to VMDisconnectedException");225log2("......trying: vm.process().destroy();");226testExitCode = FAILED;227try {228Process vmProcess = vm.process();229if (vmProcess != null) {230vmProcess.destroy();231}232} catch ( Exception e ) {233log3("ERROR: Exception : " + e);234}235break;236}237238return testExitCode;239}240241242/*243* Return value: 0 - normal end of the test244* 1 - ubnormal end of the test245* 2 - VMDisconnectedException while test phase246*/247248private int runTest() {249250try {251testRun();252253log2("waiting for VMDeathEvent");254getEventSet();255if (eventIterator.nextEvent() instanceof VMDeathEvent)256return 0;257258log3("ERROR: last event is not the VMDeathEvent");259return 1;260} catch ( VMDisconnectedException e ) {261log3("ERROR: VMDisconnectedException : " + e);262return 2;263} catch ( Exception e ) {264log3("ERROR: Exception : " + e);265return 1;266}267268}269270private void testRun()271throws JDITestRuntimeException, Exception {272273eventRManager = vm.eventRequestManager();274275ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();276cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);277cpRequest.addClassFilter(debuggeeName);278279cpRequest.enable();280vm.resume();281getEventSet();282cpRequest.disable();283284ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();285debuggeeClass = event.referenceType();286287if (!debuggeeClass.name().equals(debuggeeName))288throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");289290log2(" received: ClassPrepareEvent for debuggeeClass");291292String bPointMethod = "methodForCommunication";293String lineForComm = "lineForComm";294295ThreadReference mainThread = debuggee.threadByNameOrThrow("main");296297BreakpointRequest bpRequest = settingBreakpoint(mainThread,298debuggeeClass,299bPointMethod, lineForComm, "zero");300bpRequest.enable();301302//------------------------------------------------------ testing section303304305EventRequest eventRequest1 = null;306307String fieldName1 = "var101";308String fieldName2 = "excObj";309310ThreadReference thread1 = null;311String threadName1 = "thread1";312313ReferenceType testClassReference = null;314315316log1(" TESTING BEGINS");317318for (int i = 0; ; i++) {319320vm.resume();321breakpointForCommunication();322323int instruction = ((IntegerValue)324(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();325326if (instruction == 0) {327vm.resume();328break;329}330331log1(":::::: case: # " + i);332333//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part334335336switch (i) {337338case 0:339thread1 = debuggee.threadByNameOrThrow(threadName1);340341log2("......setting up StepRequest");342eventRequest1 = eventRManager.createStepRequest343(thread1, StepRequest.STEP_MIN, StepRequest.STEP_INTO);344break;345346case 1:347testClassReference = (ReferenceType) vm.classesByName(testedClassName).get(0);348349log2("......setting up AccessWatchpointRequest");350eventRequest1 = eventRManager.createAccessWatchpointRequest351(testClassReference.fieldByName(fieldName1));352break;353354case 2:355log2(".....setting up ModificationWatchpointRequest");356eventRequest1 = eventRManager.createModificationWatchpointRequest357(testClassReference.fieldByName(fieldName1));358break;359360case 3:361log2(".....setting up ClassPrepareRequest");362eventRequest1 = eventRManager.createClassPrepareRequest();363break;364365case 4:366log2(".....setting up ClassUnloadRequest");367eventRequest1 = eventRManager.createClassUnloadRequest();368break;369370case 5:371log2(".....setting up MethodEntryRequest");372eventRequest1 = eventRManager.createMethodEntryRequest();373break;374375case 6:376log2(".....setting up MethodExitRequest");377eventRequest1 = eventRManager.createMethodExitRequest();378break;379380case 7:381log2(".....setting up ThreadDeathRequest");382eventRequest1 = eventRManager.createThreadDeathRequest();383break;384385case 8:386log2(".....setting up ThreadStartRequest");387eventRequest1 = eventRManager.createThreadStartRequest();388break;389390case 9:391log2(".....setting up VMDeathRequest");392eventRequest1 = eventRManager.createVMDeathRequest();393break;394395case 10:396log2(".....setting up ExceptionRequest");397eventRequest1 = eventRManager.createExceptionRequest(398(ReferenceType)399(debuggeeClass.getValue(debuggeeClass.fieldByName(fieldName2))).type(),400true, true );401break;402403case 11:404log2(".....setting up BreakpointRequest");405eventRequest1 = eventRManager.createBreakpointRequest(location);406break;407408409default:410throw new JDITestRuntimeException("** default case 2 **");411}412413vm.suspend();414log2("......eventRequest1.setEnabled(true);");415eventRequest1.setEnabled(true);416417log2("......eventRequest1.disable();");418eventRequest1.disable();419420log2(" checking up on eventRequest1");421if ( eventRequest1.isEnabled() ) {422testExitCode = FAILED;423log3("ERROR: EventRequest is still enabled");424}425eventRequest1.setEnabled(false);426vm.resume();427428//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~429}430log1(" TESTING ENDS");431return;432}433434/*435* private BreakpointRequest settingBreakpoint(ThreadReference, ReferenceType,436* String, String, String)437*438* It sets up a breakpoint at given line number within a given method in a given class439* for a given thread.440*441* Return value: BreakpointRequest object in case of success442*443* JDITestRuntimeException in case of an Exception thrown within the method444*/445446private BreakpointRequest settingBreakpoint ( ThreadReference thread,447ReferenceType testedClass,448String methodName,449String bpLine,450String property)451throws JDITestRuntimeException {452453log2("......setting up a breakpoint:");454log2(" thread: " + thread + "; class: " + testedClass +455"; method: " + methodName + "; line: " + bpLine);456457List alllineLocations = null;458Location lineLocation = null;459BreakpointRequest breakpRequest = null;460461try {462Method method = (Method) testedClass.methodsByName(methodName).get(0);463464alllineLocations = method.allLineLocations();465466int n =467( (IntegerValue) testedClass.getValue(testedClass.fieldByName(bpLine) ) ).value();468if (n > alllineLocations.size()) {469log3("ERROR: TEST_ERROR_IN_settingBreakpoint(): number is out of bound of method's lines");470} else {471lineLocation = (Location) alllineLocations.get(n);472473// this is only for this test to get Location object474location = lineLocation;475476try {477breakpRequest = eventRManager.createBreakpointRequest(lineLocation);478breakpRequest.putProperty("number", property);479breakpRequest.addThreadFilter(thread);480breakpRequest.setSuspendPolicy( EventRequest.SUSPEND_ALL);481} catch ( Exception e1 ) {482log3("ERROR: inner Exception within settingBreakpoint() : " + e1);483breakpRequest = null;484}485}486} catch ( Exception e2 ) {487log3("ERROR: ATTENTION: outer Exception within settingBreakpoint() : " + e2);488breakpRequest = null;489}490491if (breakpRequest == null) {492log2(" A BREAKPOINT HAS NOT BEEN SET UP");493throw new JDITestRuntimeException("**FAILURE to set up a breakpoint**");494}495496log2(" a breakpoint has been set up");497return breakpRequest;498}499500501private void getEventSet()502throws JDITestRuntimeException {503try {504// log2(" eventSet = eventQueue.remove(waitTime);");505eventSet = eventQueue.remove(waitTime);506if (eventSet == null) {507throw new JDITestRuntimeException("** TIMEOUT while waiting for event **");508}509// log2(" eventIterator = eventSet.eventIterator;");510eventIterator = eventSet.eventIterator();511} catch ( Exception e ) {512throw new JDITestRuntimeException("** EXCEPTION while waiting for event ** : " + e);513}514}515516517private void breakpointForCommunication()518throws JDITestRuntimeException {519520log2("breakpointForCommunication");521getEventSet();522523if (eventIterator.nextEvent() instanceof BreakpointEvent)524return;525526throw new JDITestRuntimeException("** event IS NOT a breakpoint **");527}528529}530531532