Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/createStepRequest/crstepreq003.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 crstepreq003 {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 = ".crstepreq003";65private final static String debuggerName = prefix + className;66private final static String debuggeeName = debuggerName + "a";6768//------------------------------------------------------ immutable common methods6970public static void main (String argv[]) {71System.exit(run(argv, System.out) + PASS_BASE);72}7374//------------------------------------------------------ test specific fields7576static final int lineForBreakInThread = 137;77static final int[] checkedLines = { 138, 138, 178 };7879//------------------------------------------------------ mutable common methods8081public static int run (String argv[], PrintStream out) {8283int exitStatus = new crstepreq003().runThis(argv, out);84System.out.println (exitStatus == PASSED ? "TEST PASSED" : "TEST FAILED");85return exitCode;86}8788private int runThis(String argv[], PrintStream out) {8990argHandler = new ArgumentHandler(argv);91log = new Log(out, argHandler);92waitTime = argHandler.getWaitTime() * 60000;9394try {9596Binder binder = new Binder(argHandler, log);97debuggee = binder.bindToDebugee(debuggeeName);98debuggee.redirectStdout(log, "debuggee stdout> ");99debuggee.redirectStderr(log, "debuggee stderr> ");100debuggee.createIOPipe();101eventRManager = debuggee.getEventRequestManager();102103vm = debuggee.VM();104eventRManager = vm.eventRequestManager();105106debuggeeClass = waitForDebuggeeClassPrepared();107108execTest();109110debuggee.resume();111getEventSet();112if (eventIterator.nextEvent() instanceof VMDeathEvent) {113display("Waiting for the debuggee's finish...");114debuggee.waitFor();115116display("Getting the debuggee's exit status.");117int status = debuggee.getStatus();118if (status != (PASSED + PASS_BASE)) {119complain("Debuggee returned UNEXPECTED exit status: " + status);120exitCode = Consts.TEST_FAILED;121}122} else {123throw new TestBug("Last event is not the VMDeathEvent");124}125126} catch (VMDisconnectedException e) {127exitCode = Consts.TEST_FAILED;128complain("The test cancelled due to VMDisconnectedException.");129e.printStackTrace(out);130display("Trying: vm.process().destroy();");131if (vm != null) {132Process vmProcess = vm.process();133if (vmProcess != null) {134vmProcess.destroy();135}136}137138} catch (Exception e) {139exitCode = Consts.TEST_FAILED;140complain("Unexpected Exception: " + e.getMessage());141e.printStackTrace(out);142complain("The test has not finished normally. Forcing: vm.exit().");143if (vm != null) {144vm.exit(PASSED + PASS_BASE);145}146debuggee.resume();147getEventSet();148}149150return exitCode;151}152153//--------------------------------------------------------- mutable common methods154155private void execTest() {156ThreadReference mainThread = debuggee.threadByNameOrThrow("main");157/*158BreakpointRequest bpRequest = setBreakpoint( mainThread,159debuggeeClass,160"methodForCommunication",1612,162"breakpointForCommunication");163*/164BreakpointRequest bpRequest = setBreakpoint( null,165debuggeeClass,166"breakInThread",167lineForBreakInThread,168"breakInThread");169bpRequest.enable();170171display("TESTING BEGINS");172173label0:174for (int testCase = 0; instruction != quit; testCase++) {175176// waitForEvent(bpRequest);177instruction = getInstruction();178if (instruction == quit) {179vm.resume();180break;181}182183display(":: case: # " + testCase);184185switch (testCase) {186//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test case section187case 0:188display("Step request will be created with size == StepRequest.STEP_LINE, depth == StepRequest.STEP_INTO");189setAndCheckStepEvent ( bpRequest,190"StepRequest0",191"thread0",192checkedLines[testCase],193StepRequest.STEP_INTO);194break;195196case 1:197display("Step request will be created with size == StepRequest.STEP_LINE, depth == StepRequest.STEP_OVER");198setAndCheckStepEvent ( bpRequest,199"StepRequest1",200"thread1",201checkedLines[testCase],202StepRequest.STEP_OVER);203break;204case 2:205display("Step request will be created with size == StepRequest.STEP_LINE, depth == StepRequest.STEP_OUT");206setAndCheckStepEvent ( bpRequest,207"StepRequest2",208"thread2",209checkedLines[testCase],210StepRequest.STEP_OUT);211//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end of section212default:213instruction = quit;214setInstruction("quit");215}216}217display("TESTING ENDS");218}219220//--------------------------------------------------------- test specific methodss221222private StepRequest setStepRequest ( ThreadReference thread, int size, int depth, String property ) {223display("Setting a step request in thread: " + thread);224StepRequest stepRequest = null;225226try {227stepRequest = eventRManager.createStepRequest(thread, size, depth);228stepRequest.putProperty("number", property);229} catch ( Exception e1 ) {230complain("setStepRequest(): unexpected Exception while creating StepRequest: " + e1);231throw new Failure("setStep(): A StepRequest has not been set up.");232}233234display("setStepRequest(): A StepRequest has been set up.");235return stepRequest;236}237238private void setAndCheckStepEvent ( BreakpointRequest bpRequest,239String caseProperty,240String threadName,241int lineOfStepEvent,242int stepDepth ) {243display("Wait for brakepoint event in " + threadName);244BreakpointEvent bpEvent = (BreakpointEvent)waitForEvent(bpRequest);245246// check location of breakpoint event247int lineOfEvent = ((LocatableEvent)bpEvent).location().lineNumber();248if (lineOfEvent != lineForBreakInThread) {249complain("Wrong line number of BreakpointEvent for " + threadName);250complain("\texpected value : " + lineForBreakInThread + "; got one : " + lineOfEvent);251exitCode = FAILED;252}253254ThreadReference thread = debuggee.threadByNameOrThrow(threadName);255StepRequest stepRequest = setStepRequest( thread,256StepRequest.STEP_LINE,257stepDepth,258caseProperty);259stepRequest.enable();260261display("waiting for StepEvent in " + threadName);262Event newEvent = waitForEvent(stepRequest);263if (newEvent instanceof StepEvent) {264String property = (String) newEvent.request().getProperty("number");265display("got new StepEvent with property 'number' == " + property);266267if ( !property.equals(caseProperty) ) {268complain("property is not : " + caseProperty);269exitCode = FAILED;270}271// check location of step event272lineOfEvent = ((LocatableEvent)newEvent).location().lineNumber();273if (lineOfEvent != lineOfStepEvent) {274switch (stepDepth) {275case StepRequest.STEP_INTO:276complain("Wrong line number of StepEvent for request with depth == StepRequest.STEP_INTO:" );277break;278case StepRequest.STEP_OVER:279complain("Wrong line number of StepEvent for request with depth == StepRequest.STEP_OVER:" );280break;281case StepRequest.STEP_OUT:282complain("Wrong line number of StepEvent for request with depth == StepRequest.STEP_OUT:" );283break;284}285complain("\texpected value : " + lineOfStepEvent + "; got one : " + lineOfEvent);286exitCode = FAILED;287}288289} else if (newEvent instanceof BreakpointEvent) {290vm.resume();291exitCode = FAILED;292complain("got unexpected BreakpointEvent, but StepEvent is not received");293} else if (newEvent instanceof VMDeathEvent) {294exitCode = FAILED;295throw new Failure("got unexpected VMDeathtEvent, but StepEvent is not received");296}297298stepRequest.disable();299eventRManager.deleteEventRequest(stepRequest);300stepRequest = null;301display("request for StepEvent in " + threadName + " is deleted");302}303304//--------------------------------------------------------- immutable common methods305306void display(String msg) {307log.display("debugger > " + msg);308}309310void complain(String msg) {311log.complain("debugger FAILURE > " + msg);312}313314/**315* Sets up a breakpoint at given line number within a given method in a given class316* for a given thread.317*318* Returns a BreakpointRequest object in case of success, otherwise throws Failure.319*/320private BreakpointRequest setBreakpoint ( ThreadReference thread,321ReferenceType testedClass,322String methodName,323int bpLine,324String property) {325326display("Setting a breakpoint in :");327display(" thread: " + thread + "; class: " + testedClass +328"; method: " + methodName + "; line: " + bpLine + "; property: " + property);329330List allLineLocations = null;331Location lineLocation = null;332BreakpointRequest breakpRequest = null;333334try {335Method method = (Method) testedClass.methodsByName(methodName).get(0);336337allLineLocations = method.allLineLocations();338339display("Getting location for breakpoint...");340Iterator locIterator = allLineLocations.iterator();341while (locIterator.hasNext()) {342Location curLocation = (Location)locIterator.next();343int curNumber = curLocation.lineNumber();344if (curLocation.lineNumber() == bpLine) {345lineLocation = curLocation;346break;347}348}349if (lineLocation == null) {350throw new TestBug("Incorrect line number of methods' location");351}352353try {354breakpRequest = eventRManager.createBreakpointRequest(lineLocation);355if (thread != null) {356breakpRequest.addThreadFilter(thread);357}358breakpRequest.putProperty("number", property);359} catch ( Exception e1 ) {360complain("setBreakpoint(): unexpected Exception while creating BreakpointRequest: " + e1);361breakpRequest = null;362}363} catch ( Exception e2 ) {364complain("setBreakpoint(): unexpected Exception while getting locations: " + e2);365breakpRequest = null;366}367368if (breakpRequest == null) {369throw new Failure("setBreakpoint(): A breakpoint has not been set up.");370}371372display("setBreakpoint(): A breakpoint has been set up.");373return breakpRequest;374}375376private Event waitForEvent (EventRequest eventRequest) {377378vm.resume();379Event resultEvent = null;380try {381eventSet = null;382eventIterator = null;383eventSet = vm.eventQueue().remove(waitTime);384if (eventSet == null) {385throw new Failure("TIMEOUT while waiting for an event");386}387eventIterator = eventSet.eventIterator();388while (eventIterator.hasNext()) {389Event curEvent = eventIterator.nextEvent();390if (curEvent instanceof VMDisconnectEvent) {391throw new Failure("Unexpected VMDisconnectEvent received.");392} else {393EventRequest evRequest = curEvent.request();394if (evRequest != null && evRequest.equals(eventRequest)) {395display("Requested event received: " + curEvent.toString() +396"; request property: " + (String) curEvent.request().getProperty("number"));397resultEvent = curEvent;398break;399} else {400throw new Failure("Unexpected event received: " + curEvent.toString());401}402}403}404} catch (Exception e) {405throw new Failure("Unexpected exception while waiting for an event: " + e);406}407return resultEvent;408}409410411private void getEventSet() {412try {413eventSet = vm.eventQueue().remove(waitTime);414if (eventSet == null) {415throw new Failure("TIMEOUT while waiting for an event");416}417eventIterator = eventSet.eventIterator();418} catch (Exception e) {419throw new Failure("getEventSet(): Unexpected exception while waiting for an event: " + e);420}421}422423private ReferenceType waitForDebuggeeClassPrepared () {424display("Creating request for ClassPrepareEvent for debuggee.");425ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();426cpRequest.addClassFilter(debuggeeName);427cpRequest.addCountFilter(1);428cpRequest.enable();429430ClassPrepareEvent event = (ClassPrepareEvent) waitForEvent(cpRequest);431cpRequest.disable();432433if (!event.referenceType().name().equals(debuggeeName)) {434throw new Failure("Unexpected class name for ClassPrepareEvent : " + debuggeeClass.name());435}436return event.referenceType();437}438439private int getInstruction () {440if (debuggeeClass == null) {441throw new Failure("getInstruction() :: debuggeeClass reference is null");442}443return ((IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();444}445446private void setInstruction (String instructionField) {447if (debuggeeClass == null) {448throw new Failure("getInstruction() :: debuggeeClass reference is null");449}450Field instrField = debuggeeClass.fieldByName("instruction");451IntegerValue instrValue = (IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName(instructionField)));452try {453((ClassType)debuggeeClass).setValue(instrField, instrValue );454} catch (InvalidTypeException e1) {455throw new Failure("Caught unexpected InvalidTypeException while setting value '" + instructionField + "' for instruction field");456} catch (ClassNotLoadedException e2) {457throw new Failure("Caught unexpected ClassNotLoadedException while setting value '" + instructionField + "' for instruction field");458}459}460}461462463