Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/methodExitRequests/methexitreq002.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.EventRequestManager.methodExitRequests;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* EventRequestManager. <BR>39* <BR>40* The test checks that results of the method <BR>41* <code>com.sun.jdi.EventRequestManager.methodExitRequests()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks up on the following assertion: <BR>45* - The list is unmodifiable. <BR>46* - The list of the enabled and disabled method exit requests. <BR>47* - This list is changes as requests are added and deleted. <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_EVENT_THREAD <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. <BR>65* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>66* - The debuggee prepares the check case 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 checking up on assertions. <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 methexitreq002 extends JDIBase {7980public static void main (String argv[]) {8182int result = run(argv, System.out);8384System.exit(result + PASS_BASE);85}8687public static int run (String argv[], PrintStream out) {8889int exitCode = new methexitreq002().runThis(argv, out);9091if (exitCode != PASSED) {92System.out.println("TEST FAILED");93}94return testExitCode;95}96// ************************************************ test parameters9798private String debuggeeName =99"nsk.jdi.EventRequestManager.methodExitRequests.methexitreq002a";100101//====================================================== test program102103private int runThis (String argv[], PrintStream out) {104105argsHandler = new ArgumentHandler(argv);106logHandler = new Log(out, argsHandler);107Binder binder = new Binder(argsHandler, logHandler);108109waitTime = argsHandler.getWaitTime() * 60000;110111try {112log2("launching a debuggee :");113log2(" " + debuggeeName);114if (argsHandler.verbose()) {115debuggee = binder.bindToDebugee(debuggeeName + " -vbs");116} else {117debuggee = binder.bindToDebugee(debuggeeName);118}119if (debuggee == null) {120log3("ERROR: no debuggee launched");121return FAILED;122}123log2("debuggee launched");124} catch ( Exception e ) {125log3("ERROR: Exception : " + e);126log2(" test cancelled");127return FAILED;128}129130debuggee.redirectOutput(logHandler);131132vm = debuggee.VM();133134eventQueue = vm.eventQueue();135if (eventQueue == null) {136log3("ERROR: eventQueue == null : TEST ABORTED");137vm.exit(PASS_BASE);138return FAILED;139}140141log2("invocation of the method runTest()");142switch (runTest()) {143144case 0 : log2("test phase has finished normally");145log2(" waiting for the debuggee to finish ...");146debuggee.waitFor();147148log2("......getting the debuggee's exit status");149int status = debuggee.getStatus();150if (status != PASS_BASE) {151log3("ERROR: debuggee returned UNEXPECTED exit status: " +152status + " != PASS_BASE");153testExitCode = FAILED;154} else {155log2("......debuggee returned expected exit status: " +156status + " == PASS_BASE");157}158break;159160default : log3("ERROR: runTest() returned unexpected value");161162case 1 : log3("test phase has not finished normally: debuggee is still alive");163log2("......forcing: vm.exit();");164testExitCode = FAILED;165try {166vm.exit(PASS_BASE);167} catch ( Exception e ) {168log3("ERROR: Exception : " + e);169}170break;171172case 2 : log3("test cancelled due to VMDisconnectedException");173log2("......trying: vm.process().destroy();");174testExitCode = FAILED;175try {176Process vmProcess = vm.process();177if (vmProcess != null) {178vmProcess.destroy();179}180} catch ( Exception e ) {181log3("ERROR: Exception : " + e);182}183break;184}185186return testExitCode;187}188189190/*191* Return value: 0 - normal end of the test192* 1 - ubnormal end of the test193* 2 - VMDisconnectedException while test phase194*/195196private int runTest() {197198try {199testRun();200201log2("waiting for VMDeathEvent");202getEventSet();203if (eventIterator.nextEvent() instanceof VMDeathEvent)204return 0;205206log3("ERROR: last event is not the VMDeathEvent");207return 1;208} catch ( VMDisconnectedException e ) {209log3("ERROR: VMDisconnectedException : " + e);210return 2;211} catch ( Exception e ) {212log3("ERROR: Exception : " + e);213return 1;214}215216}217218private void testRun()219throws JDITestRuntimeException, Exception {220221eventRManager = vm.eventRequestManager();222223ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();224cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);225cpRequest.addClassFilter(debuggeeName);226227cpRequest.enable();228vm.resume();229getEventSet();230cpRequest.disable();231232ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();233debuggeeClass = event.referenceType();234235if (!debuggeeClass.name().equals(debuggeeName))236throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");237238log2(" received: ClassPrepareEvent for debuggeeClass");239240String bPointMethod = "methodForCommunication";241String lineForComm = "lineForComm";242243ThreadReference mainThread = debuggee.threadByNameOrThrow("main");244245BreakpointRequest bpRequest = settingBreakpoint(mainThread,246debuggeeClass,247bPointMethod, lineForComm, "zero");248bpRequest.enable();249250//------------------------------------------------------ testing section251252List requests = null;253ListIterator li = null;254255MethodExitRequest request = null;256MethodExitRequest meRequests[] = { null, null, null, null, null,257null, null, null, null, null };258int listSize;259int flag;260261262log1(" TESTING BEGINS");263264for (int i = 0; ; i++) {265266vm.resume();267breakpointForCommunication();268269int instruction = ((IntegerValue)270(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();271272if (instruction == 0) {273vm.resume();274break;275}276277log1(":::::: case: # " + i);278279//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part280281log2("......creating MethodExitRequests");282for (int i1 = 0; i1 < meRequests.length; i1++) {283meRequests[i1] = eventRManager.createMethodExitRequest();284meRequests[i1].putProperty("number", String.valueOf(i1));285286log2("......checking up on returned List after creating new MethodExitRequest");287requests = eventRManager.methodExitRequests();288listSize = requests.size();289if ( listSize != (i1 + 1) ) {290testExitCode = FAILED;291log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + (i1 + 1));292}293flag = 0;294li = requests.listIterator();295while (li.hasNext()) {296request = (MethodExitRequest) li.next();297if ( !request.isEnabled() ) {298flag++;299if (flag > 1) {300testExitCode = FAILED;301log3("ERROR: # of disabled requests > 1 : " + flag);302}303if ( !request.getProperty("number").equals(String.valueOf(i1)) ) {304testExitCode = FAILED;305log3("ERROR: in the List, disabled is request expected to be enabled : # == " + i1);306}307} else {308if ( request.getProperty("number").equals(String.valueOf(i1)) ) {309testExitCode = FAILED;310log3("ERROR: in the List, enabled is newly created disabled request : # == " + i1);311}312}313}314315log2(" enabling created MethodExitRequest");316meRequests[i1].enable();317requests = eventRManager.methodExitRequests();318listSize = requests.size();319if ( listSize != (i1 + 1) ) {320testExitCode = FAILED;321log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + (i1 + 1));322}323324li = requests.listIterator();325while (li.hasNext()) {326request = (MethodExitRequest) li.next();327if ( !request.isEnabled() ) {328testExitCode = FAILED;329log3("ERROR: returned List contains disabled request : " + request);330}331}332333log2(" removing item from the List; UnsupportedOperationException is expected");334try {335requests.remove(i1);336testExitCode = FAILED;337log3("ERROR: NO exception");338} catch ( UnsupportedOperationException e ) {339log2(" UnsupportedOperationException ");340}341}342343log2("......deleting MethodExitRequests");344for (int i2 = meRequests.length -1; i2 >= 0; i2--) {345eventRManager.deleteEventRequest(meRequests[i2]);346requests = eventRManager.methodExitRequests();347listSize = requests.size();348if ( listSize != i2 ) {349testExitCode = FAILED;350log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + i2);351}352log2(" removing item from the List; UnsupportedOperationException is expected");353try {354requests.remove(i2);355testExitCode = FAILED;356log3("ERROR: NO exception");357} catch ( UnsupportedOperationException e ) {358log2(" UnsupportedOperationException ");359}360}361362//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~363}364log1(" TESTING ENDS");365return;366}367368protected void breakpointForCommunication()369throws JDITestRuntimeException {370log2("breakpointForCommunication");371372do {373getEventSet();374375Event event = eventIterator.nextEvent();376if (event instanceof BreakpointEvent)377return;378379log2(" received: " + event);380381if (EventFilters.filtered(event, debuggeeName)) {382eventSet.resume();383}384else {385break;386}387} while (true);388389throw new JDITestRuntimeException("** event IS NOT a breakpoint **");390}391392}393394395