Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/createStepRequest/crstepreq010.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 crstepreq010 {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 = ".crstepreq010";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 = 5;78static final String[] brakeMethods = {79"caseRun",80"m01",81"m02",82"m03",83"caseRun"84};85static final int[][] checkedLines = {86{ 152, 152, 152},87{ 182, 182, 182},88{ 187, 187, 187},89{ 193, 193, 193},90{ 169, 169, 196}91};9293static final String debuggeeThreadName = prefix + ".Thread0crstepreq010a";9495//------------------------------------------------------ mutable common methods9697public static int run (String argv[], PrintStream out) {9899int exitStatus = new crstepreq010().runThis(argv, out);100System.out.println (exitStatus == PASSED ? "TEST PASSED" : "TEST FAILED");101return exitCode;102}103104private int runThis(String argv[], PrintStream out) {105106argHandler = new ArgumentHandler(argv);107log = new Log(out, argHandler);108waitTime = argHandler.getWaitTime() * 60000;109110try {111112Binder binder = new Binder(argHandler, log);113debuggee = binder.bindToDebugee(debuggeeName);114debuggee.redirectStderr(log, "");115eventRManager = debuggee.getEventRequestManager();116117vm = debuggee.VM();118eventRManager = vm.eventRequestManager();119120debuggeeClass = waitForDebuggeeClassPrepared();121122execTest();123124debuggee.resume();125getEventSet();126if (eventIterator.nextEvent() instanceof VMDeathEvent) {127display("Waiting for the debuggee's finish...");128debuggee.waitFor();129130display("Getting the debuggee's exit status.");131int status = debuggee.getStatus();132if (status != (PASSED + PASS_BASE)) {133complain("Debuggee returned UNEXPECTED exit status: " + status);134exitCode = Consts.TEST_FAILED;135}136} else {137throw new TestBug("Last event is not the VMDeathEvent");138}139140} catch (VMDisconnectedException e) {141exitCode = Consts.TEST_FAILED;142complain("The test cancelled due to VMDisconnectedException.");143e.printStackTrace(out);144display("Trying: vm.process().destroy();");145if (vm != null) {146Process vmProcess = vm.process();147if (vmProcess != null) {148vmProcess.destroy();149}150}151152} catch (Exception e) {153exitCode = Consts.TEST_FAILED;154complain("Unexpected Exception: " + e.getMessage());155e.printStackTrace(out);156complain("The test has not finished normally. Forcing: vm.exit().");157if (vm != null) {158vm.exit(PASSED + PASS_BASE);159}160debuggee.resume();161getEventSet();162}163164return exitCode;165}166167//--------------------------------------------------------- mutable common methods168169private void execTest() {170BreakpointRequest bpRequest = setBreakpoint( null,171debuggeeClass,172"methodForCommunication",173lineForBreak,174"breakForCommunication");175bpRequest.enable();176177StepRequest stepRequest = null;178179display("TESTING BEGINS");180for (int testCase = 0; testCase < maxCase && instruction != quit; testCase++) {181182instruction = getInstruction();183if (instruction == quit) {184vm.resume();185break;186}187188display(":: CASE # " + testCase);189stepRequest = setStepRequest( bpRequest,190"thread" + testCase,191testCase,192"stepRequest" + testCase );193194checkStepEvent( stepRequest,195"thread" + testCase,196testCase );197}198display("TESTING ENDS");199}200201//--------------------------------------------------------- test specific methods202203private StepRequest setStepRequest ( BreakpointRequest bpRequest,204String threadName,205int testCase,206String property ) {207StepRequest stepRequest = null;208for (;;) {209display("Wait for initial brakepoint event in " + threadName);210BreakpointEvent bpEvent = (BreakpointEvent)waitForEvent(bpRequest);211212// check location of breakpoint event213int lineOfEvent = ((LocatableEvent)bpEvent).location().lineNumber();214if (lineOfEvent != lineForBreak) {215complain("Wrong line number of initial brakepoint event for " + threadName);216complain("\texpected value : " + lineForBreak + "; got one : " + lineOfEvent);217break;218}219220display("Getting mirror of thread: " + threadName);221ThreadReference thread = debuggee.threadByNameOrThrow(threadName);222223display("Getting ReferenceType of thread: " + threadName);224ReferenceType debuggeeThread = debuggee.classByName(debuggeeThreadName);225226// set second breakpoint to suspend checked thread at the right location before227// setting step request228BreakpointRequest bpRequest1 = setBreakpoint( thread,229debuggeeThread,230brakeMethods[testCase],231checkedLines[testCase][0],232"");233bpRequest1.addCountFilter(1);234bpRequest1.enable();235236display("Wait for additional brakepoint event in " + threadName);237bpEvent = (BreakpointEvent)waitForEvent(bpRequest1);238239// check location of breakpoint event240lineOfEvent = ((LocatableEvent)bpEvent).location().lineNumber();241if (lineOfEvent != checkedLines[testCase][0]) {242complain("Wrong line number of additional brakepoint event for " + threadName);243complain("\texpected value : " + checkedLines[testCase][0] + "; got one : " + lineOfEvent);244break;245}246247display("Setting a step request in thread: " + thread);248try {249stepRequest = eventRManager.createStepRequest ( thread,250StepRequest.STEP_MIN,251StepRequest.STEP_INTO );252stepRequest.putProperty("number", property);253} catch ( Exception e1 ) {254complain("setStepRequest(): unexpected Exception while creating StepRequest: " + e1);255break;256}257break;258}259if (stepRequest == null) {260throw new Failure("setStepRequest(): StepRequest has not been set up.");261}262display("setStepRequest(): StepRequest has been set up.");263return stepRequest;264}265266private void checkStepEvent ( StepRequest stepRequest,267String threadName,268int testCase ) {269stepRequest.enable();270271display("waiting for first StepEvent in " + threadName);272Event newEvent = waitForEvent(stepRequest);273display("got first StepEvent");274275display("CHECK1 for line location of first StepEvent.");276int lineOfEvent = ((LocatableEvent)newEvent).location().lineNumber();277if (lineOfEvent != checkedLines[testCase][1]) {278complain("CHECK1 for line location of first StepEvent FAILED for CASE # " + testCase);279complain("\texpected value : " + checkedLines[testCase][1] + "; got one : " + lineOfEvent);280exitCode = FAILED;281} else {282display("CHECK1 PASSED");283}284285display("waiting for second StepEvent in " + threadName);286newEvent = waitForEvent(stepRequest);287display("got second StepEvent");288289display("CHECK2 for line location of second StepEvent.");290lineOfEvent = ((LocatableEvent)newEvent).location().lineNumber();291if (lineOfEvent != checkedLines[testCase][2]) {292complain("CHECK2 for line location of second StepEvent FAILED for CASE # " + testCase);293complain("\texpected value : " + checkedLines[testCase][2] + "; got one : " + lineOfEvent);294exitCode = FAILED;295} else {296display("CHECK2 PASSED");297}298299stepRequest.disable();300eventRManager.deleteEventRequest(stepRequest);301stepRequest = null;302display("request for StepEvent in " + threadName + " is deleted");303}304305//--------------------------------------------------------- immutable common methods306307void display(String msg) {308log.display("debugger > " + msg);309}310311void complain(String msg) {312log.complain("debugger FAILURE > " + msg);313}314315/**316* Sets up a breakpoint at given line number within a given method in a given class317* for a given thread.318*319* Returns a BreakpointRequest object in case of success, otherwise throws Failure.320*/321private BreakpointRequest setBreakpoint ( ThreadReference thread,322ReferenceType testedClass,323String methodName,324int bpLine,325String property) {326327display("Setting a breakpoint in :");328display(" thread: " + thread + "; class: " + testedClass +329"; method: " + methodName + "; line: " + bpLine + "; property: " + property);330331List allLineLocations = null;332Location lineLocation = null;333BreakpointRequest breakpRequest = null;334335try {336Method method = (Method) testedClass.methodsByName(methodName).get(0);337338allLineLocations = method.allLineLocations();339340display("Getting location for breakpoint...");341Iterator locIterator = allLineLocations.iterator();342while (locIterator.hasNext()) {343Location curLocation = (Location)locIterator.next();344int curNumber = curLocation.lineNumber();345if (curLocation.lineNumber() == bpLine) {346lineLocation = curLocation;347break;348}349}350if (lineLocation == null) {351throw new TestBug("Incorrect line number of methods' location");352}353354try {355breakpRequest = eventRManager.createBreakpointRequest(lineLocation);356if (thread != null) {357breakpRequest.addThreadFilter(thread);358}359breakpRequest.putProperty("number", property);360} catch ( Exception e1 ) {361complain("setBreakpoint(): unexpected Exception while creating BreakpointRequest: " + e1);362breakpRequest = null;363}364} catch ( Exception e2 ) {365complain("setBreakpoint(): unexpected Exception while getting locations: " + e2);366breakpRequest = null;367}368369if (breakpRequest == null) {370throw new Failure("setBreakpoint(): A breakpoint has not been set up.");371}372373display("setBreakpoint(): A breakpoint has been set up.");374return breakpRequest;375}376377private Event waitForEvent (EventRequest eventRequest) {378vm.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}409410private Event waitForEvent () {411vm.resume();412Event resultEvent = null;413try {414eventSet = null;415eventIterator = null;416eventSet = vm.eventQueue().remove(waitTime);417if (eventSet == null) {418throw new Failure("TIMEOUT while waiting for an event");419}420eventIterator = eventSet.eventIterator();421while (eventIterator.hasNext()) {422resultEvent = eventIterator.nextEvent();423if (resultEvent instanceof VMDisconnectEvent) {424throw new Failure("Unexpected VMDisconnectEvent received.");425}426}427} catch (Exception e) {428throw new Failure("Unexpected exception while waiting for an event: " + e);429}430return resultEvent;431}432433private void getEventSet() {434try {435eventSet = vm.eventQueue().remove(waitTime);436if (eventSet == null) {437throw new Failure("TIMEOUT while waiting for an event");438}439eventIterator = eventSet.eventIterator();440} catch (Exception e) {441throw new Failure("getEventSet(): Unexpected exception while waiting for an event: " + e);442}443}444445private ReferenceType waitForDebuggeeClassPrepared () {446display("Creating request for ClassPrepareEvent for debuggee.");447ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();448cpRequest.addClassFilter(debuggeeName);449cpRequest.addCountFilter(1);450cpRequest.enable();451452ClassPrepareEvent event = (ClassPrepareEvent) waitForEvent(cpRequest);453cpRequest.disable();454455if (!event.referenceType().name().equals(debuggeeName)) {456throw new Failure("Unexpected class name for ClassPrepareEvent : " + debuggeeClass.name());457}458return event.referenceType();459}460461private int getInstruction () {462if (debuggeeClass == null) {463throw new Failure("getInstruction() :: debuggeeClass reference is null");464}465return ((IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();466}467468private void setInstruction (String instructionField) {469if (debuggeeClass == null) {470throw new Failure("getInstruction() :: debuggeeClass reference is null");471}472Field instrField = debuggeeClass.fieldByName("instruction");473IntegerValue instrValue = (IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName(instructionField)));474try {475((ClassType)debuggeeClass).setValue(instrField, instrValue );476} catch (InvalidTypeException e1) {477throw new Failure("Caught unexpected InvalidTypeException while setting value '" + instructionField + "' for instruction field");478} catch (ClassNotLoadedException e2) {479throw new Failure("Caught unexpected ClassNotLoadedException while setting value '" + instructionField + "' for instruction field");480}481}482}483484485