Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequest/setEnabled/setenabled001.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* Enables or disables this event request. <BR>46* The cases to test include all sub-types of EventRequest. <BR>47* <BR>48* The test has three phases and works as follows. <BR>49* <BR>50* In first phase, <BR>51* upon launching debuggee's VM which will be suspended, <BR>52* a debugger waits for the VMStartEvent within a predefined <BR>53* time interval. If no the VMStartEvent received, the test is FAILED. <BR>54* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>55* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>56* and waits for the event within the predefined time interval. <BR>57* If no the ClassPrepareEvent received, the test is FAILED. <BR>58* Upon getting the ClassPrepareEvent, <BR>59* the debugger sets up the breakpoint with SUSPEND_ALL <BR>60* within debuggee's special methodForCommunication(). <BR>61* <BR>62* In second phase to check the assertion, <BR>63* the debugger and the debuggee perform the following. <BR>64* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>65* - The debuggee prepares new check case and invokes <BR>66* the methodForCommunication to be suspended and <BR>67* to inform the debugger with the event. <BR>68* - Upon getting the BreakpointEvent, <BR>69* the debugger performs the check case required. <BR>70* <BR>71* In third phase, at the end of the test, the debuggee changes <BR>72* the value of the "instruction" which the debugger and debuggee <BR>73* use to inform each other of needed actions, and both end. <BR>74* <BR>75*/7677public class setenabled001 extends JDIBase {7879public static void main (String argv[]) {8081int result = run(argv, System.out);8283System.exit(result + PASS_BASE);84}8586public static int run (String argv[], PrintStream out) {8788int exitCode = new setenabled001().runThis(argv, out);8990if (exitCode != PASSED) {91System.out.println("TEST FAILED");92}93return testExitCode;94}9596// ************************************************ test parameters9798private String debuggeeName =99"nsk.jdi.EventRequest.setEnabled.setenabled001a";100101private String testedClassName =102"nsk.jdi.EventRequest.setEnabled.setenabled001aTestClass11";103104//====================================================== test program105106private int runThis (String argv[], PrintStream out) {107108argsHandler = new ArgumentHandler(argv);109logHandler = new Log(out, argsHandler);110Binder binder = new Binder(argsHandler, logHandler);111112waitTime = argsHandler.getWaitTime() * 60000;113114try {115log2("launching a debuggee :");116log2(" " + debuggeeName);117if (argsHandler.verbose()) {118debuggee = binder.bindToDebugee(debuggeeName + " -vbs");119} else {120debuggee = binder.bindToDebugee(debuggeeName);121}122if (debuggee == null) {123log3("ERROR: no debuggee launched");124return FAILED;125}126log2("debuggee launched");127} catch ( Exception e ) {128log3("ERROR: Exception : " + e);129log2(" test cancelled");130return FAILED;131}132133debuggee.redirectOutput(logHandler);134135vm = debuggee.VM();136137eventQueue = vm.eventQueue();138if (eventQueue == null) {139log3("ERROR: eventQueue == null : TEST ABORTED");140vm.exit(PASS_BASE);141return FAILED;142}143144log2("invocation of the method runTest()");145switch (runTest()) {146147case 0 : log2("test phase has finished normally");148log2(" waiting for the debuggee to finish ...");149debuggee.waitFor();150151log2("......getting the debuggee's exit status");152int status = debuggee.getStatus();153if (status != PASS_BASE) {154log3("ERROR: debuggee returned UNEXPECTED exit status: " +155status + " != PASS_BASE");156testExitCode = FAILED;157} else {158log2("......debuggee returned expected exit status: " +159status + " == PASS_BASE");160}161break;162163default : log3("ERROR: runTest() returned unexpected value");164165case 1 : log3("test phase has not finished normally: debuggee is still alive");166log2("......forcing: vm.exit();");167testExitCode = FAILED;168try {169vm.exit(PASS_BASE);170} catch ( Exception e ) {171log3("ERROR: Exception : " + e);172}173break;174175case 2 : log3("test cancelled due to VMDisconnectedException");176log2("......trying: vm.process().destroy();");177testExitCode = FAILED;178try {179Process vmProcess = vm.process();180if (vmProcess != null) {181vmProcess.destroy();182}183} catch ( Exception e ) {184log3("ERROR: Exception : " + e);185}186break;187}188189return testExitCode;190}191192193/*194* Return value: 0 - normal end of the test195* 1 - ubnormal end of the test196* 2 - VMDisconnectedException while test phase197*/198199private int runTest() {200201try {202testRun();203204log2("waiting for VMDeathEvent");205getEventSet();206if (eventIterator.nextEvent() instanceof VMDeathEvent)207return 0;208209log3("ERROR: last event is not the VMDeathEvent");210return 1;211} catch ( VMDisconnectedException e ) {212log3("ERROR: VMDisconnectedException : " + e);213return 2;214} catch ( Exception e ) {215log3("ERROR: Exception : " + e);216return 1;217}218219}220221private void testRun()222throws JDITestRuntimeException, Exception {223224eventRManager = vm.eventRequestManager();225226ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();227cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);228cpRequest.addClassFilter(debuggeeName);229230cpRequest.enable();231vm.resume();232getEventSet();233cpRequest.disable();234235ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();236debuggeeClass = event.referenceType();237238if (!debuggeeClass.name().equals(debuggeeName))239throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");240241log2(" received: ClassPrepareEvent for debuggeeClass");242243String bPointMethod = "methodForCommunication";244String lineForComm = "lineForComm";245246ThreadReference mainThread = debuggee.threadByNameOrThrow("main");247248BreakpointRequest bpRequest = settingBreakpoint(mainThread,249debuggeeClass,250bPointMethod, lineForComm, "zero");251bpRequest.enable();252253//------------------------------------------------------ testing section254255256EventRequest eventRequest1 = null;257258String fieldName1 = "var101";259String fieldName2 = "excObj";260261ThreadReference thread1 = null;262String threadName1 = "thread1";263264ReferenceType testClassReference = null;265266267log1(" TESTING BEGINS");268269for (int i = 0; ; i++) {270271vm.resume();272breakpointForCommunication();273274int instruction = ((IntegerValue)275(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();276277if (instruction == 0) {278vm.resume();279break;280}281282log1(":::::: case: # " + i);283284//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part285286287switch (i) {288289case 0:290thread1 = debuggee.threadByNameOrThrow(threadName1);291292log2("......setting up StepRequest");293eventRequest1 = eventRManager.createStepRequest294(thread1, StepRequest.STEP_MIN, StepRequest.STEP_INTO);295break;296297case 1:298testClassReference = (ReferenceType) vm.classesByName(testedClassName).get(0);299300log2("......setting up AccessWatchpointRequest");301eventRequest1 = eventRManager.createAccessWatchpointRequest302(testClassReference.fieldByName(fieldName1));303break;304305case 2:306log2(".....setting up ModificationWatchpointRequest");307eventRequest1 = eventRManager.createModificationWatchpointRequest308(testClassReference.fieldByName(fieldName1));309break;310311case 3:312log2(".....setting up ClassPrepareRequest");313eventRequest1 = eventRManager.createClassPrepareRequest();314break;315316case 4:317log2(".....setting up ClassUnloadRequest");318eventRequest1 = eventRManager.createClassUnloadRequest();319break;320321case 5:322log2(".....setting up MethodEntryRequest");323eventRequest1 = eventRManager.createMethodEntryRequest();324break;325326case 6:327log2(".....setting up MethodExitRequest");328eventRequest1 = eventRManager.createMethodExitRequest();329break;330331case 7:332log2(".....setting up ThreadDeathRequest");333eventRequest1 = eventRManager.createThreadDeathRequest();334break;335336case 8:337log2(".....setting up ThreadStartRequest");338eventRequest1 = eventRManager.createThreadStartRequest();339break;340341case 9:342log2(".....setting up VMDeathRequest");343eventRequest1 = eventRManager.createVMDeathRequest();344break;345346case 10:347log2(".....setting up ExceptionRequest");348eventRequest1 = eventRManager.createExceptionRequest(349(ReferenceType)350(debuggeeClass.getValue(debuggeeClass.fieldByName(fieldName2))).type(),351true, true );352break;353354case 11:355log2(".....setting up BreakpointRequest");356eventRequest1 = eventRManager.createBreakpointRequest(breakpLocation);357break;358359360default:361throw new JDITestRuntimeException("** default case 2 **");362}363364vm.suspend();365log2("......eventRequest1.setEnable(true);");366eventRequest1.setEnabled(true);367log2(" checking up on eventRequest1");368if ( !eventRequest1.isEnabled() ) {369testExitCode = FAILED;370log3("ERROR: EventRequest is not enabled");371}372373log2("......eventRequest1.setEnable(false);");374eventRequest1.setEnabled(false);375log2(" checking up on eventRequest1");376if ( eventRequest1.isEnabled() ) {377testExitCode = FAILED;378log3("ERROR: EventRequest is enabled");379}380vm.resume();381382//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~383}384log1(" TESTING ENDS");385return;386}387388}389390391