Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/createStepRequest/crstepreq006.java
41161 views
/*1* Copyright (c) 2002, 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.EventRequestManager.createStepRequest;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import com.sun.jdi.connect.*;31import com.sun.jdi.request.*;32import com.sun.jdi.event.*;33import java.io.*;34import java.util.*;3536/**37*/38public class crstepreq006 {3940//----------------------------------------------------- immutable common fields4142static final int PASSED = 0;43static final int FAILED = 2;44static final int PASS_BASE = 95;45static final int quit = -1;4647private int instruction = 1;48private int waitTime;49private static int exitCode = PASSED;5051private ArgumentHandler argHandler;52private Log log;53private Debugee debuggee;54private VirtualMachine vm;55private ReferenceType debuggeeClass;5657private EventRequestManager eventRManager;58private EventSet eventSet;59private EventIterator eventIterator;6061//------------------------------------------------------ mutable common fields6263private final static String prefix = "nsk.jdi.EventRequestManager.createStepRequest";64private final static String className = ".crstepreq006";65private final static String debuggerName = prefix + className;66private final static String debuggeeName = debuggerName + "a";67static final int lineForBreak = 62;6869//------------------------------------------------------ immutable common methods7071public static void main (String argv[]) {72System.exit(run(argv, System.out) + PASS_BASE);73}7475//------------------------------------------------------ test specific fields7677static final int maxCase = 3;78static final String[] brakeMethods = {79"m00",80"m00",81"m00"82};83static final int[][] checkedLines = {84{ 167, 171, 151},85{ 167, 175, 156},86{ 167, 178, 187}87};8889static final String debuggeeThreadName = prefix + ".Thread0crstepreq006a";9091//------------------------------------------------------ mutable common methods9293public static int run (String argv[], PrintStream out) {9495int exitStatus = new crstepreq006().runThis(argv, out);96System.out.println (exitStatus == PASSED ? "TEST PASSED" : "TEST FAILED");97return exitCode;98}99100private int runThis(String argv[], PrintStream out) {101102argHandler = new ArgumentHandler(argv);103log = new Log(out, argHandler);104waitTime = argHandler.getWaitTime() * 60000;105106try {107108Binder binder = new Binder(argHandler, log);109debuggee = binder.bindToDebugee(debuggeeName);110debuggee.redirectStderr(log, "");111eventRManager = debuggee.getEventRequestManager();112113vm = debuggee.VM();114eventRManager = vm.eventRequestManager();115116debuggeeClass = waitForDebuggeeClassPrepared();117118execTest();119120debuggee.resume();121getEventSet();122if (eventIterator.nextEvent() instanceof VMDeathEvent) {123display("Waiting for the debuggee's finish...");124debuggee.waitFor();125126display("Getting the debuggee's exit status.");127int status = debuggee.getStatus();128if (status != (PASSED + PASS_BASE)) {129complain("Debuggee returned UNEXPECTED exit status: " + status);130exitCode = Consts.TEST_FAILED;131}132} else {133throw new TestBug("Last event is not the VMDeathEvent");134}135136} catch (VMDisconnectedException e) {137exitCode = Consts.TEST_FAILED;138complain("The test cancelled due to VMDisconnectedException.");139e.printStackTrace(out);140display("Trying: vm.process().destroy();");141if (vm != null) {142Process vmProcess = vm.process();143if (vmProcess != null) {144vmProcess.destroy();145}146}147148} catch (Exception e) {149exitCode = Consts.TEST_FAILED;150complain("Unexpected Exception: " + e.getMessage());151e.printStackTrace(out);152complain("The test has not finished normally. Forcing: vm.exit().");153if (vm != null) {154vm.exit(PASSED + PASS_BASE);155}156debuggee.resume();157getEventSet();158}159160return exitCode;161}162163//--------------------------------------------------------- mutable common methods164165private void execTest() {166BreakpointRequest bpRequest = setBreakpoint( null,167debuggeeClass,168"methodForCommunication",169lineForBreak,170"breakForCommunication");171bpRequest.enable();172173StepRequest stepRequest = null;174175display("TESTING BEGINS");176for (int testCase = 0; testCase < maxCase && instruction != quit; testCase++) {177178instruction = getInstruction();179if (instruction == quit) {180vm.resume();181break;182}183184display(":: CASE # " + testCase);185stepRequest = setStepRequest( bpRequest,186"thread" + testCase,187testCase,188"stepRequest" + testCase );189190checkStepEvent( stepRequest,191"thread" + testCase,192testCase );193}194display("TESTING ENDS");195}196197//--------------------------------------------------------- test specific methods198199private StepRequest setStepRequest ( BreakpointRequest bpRequest,200String threadName,201int testCase,202String property ) {203StepRequest stepRequest = null;204for (;;) {205display("Wait for initial brakepoint event in " + threadName);206BreakpointEvent bpEvent = (BreakpointEvent)waitForEvent(bpRequest);207208// check location of breakpoint event209int lineOfEvent = ((LocatableEvent)bpEvent).location().lineNumber();210if (lineOfEvent != lineForBreak) {211complain("Wrong line number of initial brakepoint event for " + threadName);212complain("\texpected value : " + lineForBreak + "; got one : " + lineOfEvent);213break;214}215216display("Getting mirror of thread: " + threadName);217ThreadReference thread = debuggee.threadByNameOrThrow(threadName);218219display("Getting ReferenceType of thread: " + threadName);220ReferenceType debuggeeThread = debuggee.classByName(debuggeeThreadName);221222// set second breakpoint to suspend checked thread at the right location before223// setting step request224BreakpointRequest bpRequest1 = setBreakpoint( thread,225debuggeeThread,226brakeMethods[testCase],227checkedLines[testCase][0],228"");229bpRequest1.addCountFilter(1);230bpRequest1.enable();231232display("Wait for additional brakepoint event in " + threadName);233bpEvent = (BreakpointEvent)waitForEvent(bpRequest1);234235// check location of breakpoint event236lineOfEvent = ((LocatableEvent)bpEvent).location().lineNumber();237if (lineOfEvent != checkedLines[testCase][0]) {238complain("Wrong line number of additional brakepoint event for " + threadName);239complain("\texpected value : " + checkedLines[testCase][0] + "; got one : " + lineOfEvent);240break;241}242243display("Setting a step request in thread: " + thread);244try {245stepRequest = eventRManager.createStepRequest ( thread,246StepRequest.STEP_LINE,247StepRequest.STEP_OUT );248stepRequest.putProperty("number", property);249} catch ( Exception e1 ) {250complain("setStepRequest(): unexpected Exception while creating StepRequest: " + e1);251break;252}253break;254}255if (stepRequest == null) {256throw new Failure("setStepRequest(): StepRequest has not been set up.");257}258display("setStepRequest(): StepRequest has been set up.");259return stepRequest;260}261262private void checkStepEvent ( StepRequest stepRequest,263String threadName,264int testCase ) {265stepRequest.enable();266267display("waiting for first StepEvent in " + threadName);268Event newEvent = waitForEvent(stepRequest);269display("got first StepEvent");270271display("CHECK1 for line location of first StepEvent.");272int lineOfEvent = ((LocatableEvent)newEvent).location().lineNumber();273if (lineOfEvent != checkedLines[testCase][1]) {274complain("CHECK1 for line location of first StepEvent FAILED for CASE # " + testCase);275complain("\texpected value : " + checkedLines[testCase][1] + "; got one : " + lineOfEvent);276exitCode = FAILED;277} else {278display("CHECK1 PASSED");279}280281display("waiting for second StepEvent in " + threadName);282newEvent = waitForEvent(stepRequest);283display("got second StepEvent");284285display("CHECK2 for line location of second StepEvent.");286lineOfEvent = ((LocatableEvent)newEvent).location().lineNumber();287if (lineOfEvent != checkedLines[testCase][2]) {288complain("CHECK2 for line location of second StepEvent FAILED for CASE # " + testCase);289complain("\texpected value : " + checkedLines[testCase][2] + "; got one : " + lineOfEvent);290exitCode = FAILED;291} else {292display("CHECK2 PASSED");293}294295stepRequest.disable();296eventRManager.deleteEventRequest(stepRequest);297stepRequest = null;298display("request for StepEvent in " + threadName + " is deleted");299}300301//--------------------------------------------------------- immutable common methods302303void display(String msg) {304log.display("debugger > " + msg);305}306307void complain(String msg) {308log.complain("debugger FAILURE > " + msg);309}310311/**312* Sets up a breakpoint at given line number within a given method in a given class313* for a given thread.314*315* Returns a BreakpointRequest object in case of success, otherwise throws Failure.316*/317private BreakpointRequest setBreakpoint ( ThreadReference thread,318ReferenceType testedClass,319String methodName,320int bpLine,321String property) {322323display("Setting a breakpoint in :");324display(" thread: " + thread + "; class: " + testedClass +325"; method: " + methodName + "; line: " + bpLine + "; property: " + property);326327List allLineLocations = null;328Location lineLocation = null;329BreakpointRequest breakpRequest = null;330331try {332Method method = (Method) testedClass.methodsByName(methodName).get(0);333334allLineLocations = method.allLineLocations();335336display("Getting location for breakpoint...");337Iterator locIterator = allLineLocations.iterator();338while (locIterator.hasNext()) {339Location curLocation = (Location)locIterator.next();340int curNumber = curLocation.lineNumber();341if (curLocation.lineNumber() == bpLine) {342lineLocation = curLocation;343break;344}345}346if (lineLocation == null) {347throw new TestBug("Incorrect line number of methods' location");348}349350try {351breakpRequest = eventRManager.createBreakpointRequest(lineLocation);352if (thread != null) {353breakpRequest.addThreadFilter(thread);354}355breakpRequest.putProperty("number", property);356} catch ( Exception e1 ) {357complain("setBreakpoint(): unexpected Exception while creating BreakpointRequest: " + e1);358breakpRequest = null;359}360} catch ( Exception e2 ) {361complain("setBreakpoint(): unexpected Exception while getting locations: " + e2);362breakpRequest = null;363}364365if (breakpRequest == null) {366throw new Failure("setBreakpoint(): A breakpoint has not been set up.");367}368369display("setBreakpoint(): A breakpoint has been set up.");370return breakpRequest;371}372373private Event waitForEvent (EventRequest eventRequest) {374vm.resume();375Event resultEvent = null;376try {377eventSet = null;378eventIterator = null;379eventSet = vm.eventQueue().remove(waitTime);380if (eventSet == null) {381throw new Failure("TIMEOUT while waiting for an event");382}383eventIterator = eventSet.eventIterator();384while (eventIterator.hasNext()) {385Event curEvent = eventIterator.nextEvent();386if (curEvent instanceof VMDisconnectEvent) {387throw new Failure("Unexpected VMDisconnectEvent received.");388} else {389EventRequest evRequest = curEvent.request();390if (evRequest != null && evRequest.equals(eventRequest)) {391display("Requested event received: " + curEvent.toString() +392"; request property: " + (String) curEvent.request().getProperty("number"));393resultEvent = curEvent;394break;395} else {396throw new Failure("Unexpected event received: " + curEvent.toString());397}398}399}400} catch (Exception e) {401throw new Failure("Unexpected exception while waiting for an event: " + e);402}403return resultEvent;404}405406private Event waitForEvent () {407vm.resume();408Event resultEvent = null;409try {410eventSet = null;411eventIterator = null;412eventSet = vm.eventQueue().remove(waitTime);413if (eventSet == null) {414throw new Failure("TIMEOUT while waiting for an event");415}416eventIterator = eventSet.eventIterator();417while (eventIterator.hasNext()) {418resultEvent = eventIterator.nextEvent();419if (resultEvent instanceof VMDisconnectEvent) {420throw new Failure("Unexpected VMDisconnectEvent received.");421}422}423} catch (Exception e) {424throw new Failure("Unexpected exception while waiting for an event: " + e);425}426return resultEvent;427}428429private void getEventSet() {430try {431eventSet = vm.eventQueue().remove(waitTime);432if (eventSet == null) {433throw new Failure("TIMEOUT while waiting for an event");434}435eventIterator = eventSet.eventIterator();436} catch (Exception e) {437throw new Failure("getEventSet(): Unexpected exception while waiting for an event: " + e);438}439}440441private ReferenceType waitForDebuggeeClassPrepared () {442display("Creating request for ClassPrepareEvent for debuggee.");443ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();444cpRequest.addClassFilter(debuggeeName);445cpRequest.addCountFilter(1);446cpRequest.enable();447448ClassPrepareEvent event = (ClassPrepareEvent) waitForEvent(cpRequest);449cpRequest.disable();450451if (!event.referenceType().name().equals(debuggeeName)) {452throw new Failure("Unexpected class name for ClassPrepareEvent : " + debuggeeClass.name());453}454return event.referenceType();455}456457private int getInstruction () {458if (debuggeeClass == null) {459throw new Failure("getInstruction() :: debuggeeClass reference is null");460}461return ((IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();462}463464private void setInstruction (String instructionField) {465if (debuggeeClass == null) {466throw new Failure("getInstruction() :: debuggeeClass reference is null");467}468Field instrField = debuggeeClass.fieldByName("instruction");469IntegerValue instrValue = (IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName(instructionField)));470try {471((ClassType)debuggeeClass).setValue(instrField, instrValue );472} catch (InvalidTypeException e1) {473throw new Failure("Caught unexpected InvalidTypeException while setting value '" + instructionField + "' for instruction field");474} catch (ClassNotLoadedException e2) {475throw new Failure("Caught unexpected ClassNotLoadedException while setting value '" + instructionField + "' for instruction field");476}477}478}479480481