Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/createStepRequest/crstepreq004.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 crstepreq004 {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 = ".crstepreq004";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 = 146;77static final int[] checkedLines = { 160, 160, 193 };78static final int[] checkedLinesAlt = { 161, 161, 193 };7980//------------------------------------------------------ mutable common methods8182public static int run (String argv[], PrintStream out) {8384int exitStatus = new crstepreq004().runThis(argv, out);85System.out.println (exitStatus == PASSED ? "TEST PASSED" : "TEST FAILED");86return exitCode;87}8889private int runThis(String argv[], PrintStream out) {9091argHandler = new ArgumentHandler(argv);92log = new Log(out, argHandler);93waitTime = argHandler.getWaitTime() * 60000;9495try {9697Binder binder = new Binder(argHandler, log);98debuggee = binder.bindToDebugee(debuggeeName);99debuggee.redirectStdout(log, "debuggee stdout> ");100debuggee.redirectStderr(log, "debuggee stderr> ");101debuggee.createIOPipe();102eventRManager = debuggee.getEventRequestManager();103104vm = debuggee.VM();105eventRManager = vm.eventRequestManager();106107debuggeeClass = waitForDebuggeeClassPrepared();108109execTest();110111debuggee.resume();112getEventSet();113if (eventIterator.nextEvent() instanceof VMDeathEvent) {114display("Waiting for the debuggee's finish...");115debuggee.waitFor();116117display("Getting the debuggee's exit status.");118int status = debuggee.getStatus();119if (status != (PASSED + PASS_BASE)) {120complain("Debuggee returned UNEXPECTED exit status: " + status);121exitCode = Consts.TEST_FAILED;122}123} else {124throw new TestBug("Last event is not the VMDeathEvent");125}126127} catch (VMDisconnectedException e) {128exitCode = Consts.TEST_FAILED;129complain("The test cancelled due to VMDisconnectedException.");130e.printStackTrace(out);131display("Trying: vm.process().destroy();");132if (vm != null) {133Process vmProcess = vm.process();134if (vmProcess != null) {135vmProcess.destroy();136}137}138139} catch (Exception e) {140exitCode = Consts.TEST_FAILED;141complain("Unexpected Exception: " + e.getMessage());142e.printStackTrace(out);143complain("The test has not finished normally. Forcing: vm.exit().");144if (vm != null) {145vm.exit(PASSED + PASS_BASE);146}147debuggee.resume();148getEventSet();149}150151return exitCode;152}153154//--------------------------------------------------------- mutable common methods155156private void execTest() {157ThreadReference mainThread = debuggee.threadByNameOrThrow("main");158159BreakpointRequest bpRequest = setBreakpoint( mainThread,160debuggeeClass,161"methodForCommunication",162lineForBreakInThread,163"breakpointForCommunication");164bpRequest.enable();165166display("TESTING BEGINS");167168label0:169for (int testCase = 0; instruction != quit; testCase++) {170171// waitForEvent(bpRequest);172instruction = getInstruction();173if (instruction == quit) {174vm.resume();175break;176}177178display(":: case: # " + testCase);179180switch (testCase) {181//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test case section182case 0:183display("Step request will be created with size == StepRequest.STEP_LINE, depth == StepRequest.STEP_INTO");184setAndCheckStepEvent ( bpRequest,185"StepRequest0",186"thread2",187testCase,188StepRequest.STEP_INTO);189break;190191case 1:192display("Step request will be created with size == StepRequest.STEP_LINE, depth == StepRequest.STEP_OVER");193setAndCheckStepEvent ( bpRequest,194"StepRequest1",195"thread2",196testCase,197StepRequest.STEP_OVER);198break;199case 2:200display("Step request will be created with size == StepRequest.STEP_LINE, depth == StepRequest.STEP_OUT");201setAndCheckStepEvent ( bpRequest,202"StepRequest2",203"thread2",204testCase,205StepRequest.STEP_OUT);206//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end of section207default:208instruction = quit;209setInstruction("quit");210}211}212display("TESTING ENDS");213}214215//--------------------------------------------------------- test specific methodss216217private StepRequest setStepRequest ( ThreadReference thread, int size, int depth, String property ) {218display("Setting a step request in thread: " + thread);219StepRequest stepRequest = null;220221try {222stepRequest = eventRManager.createStepRequest(thread, size, depth);223stepRequest.putProperty("number", property);224} catch ( Exception e1 ) {225complain("setStepRequest(): unexpected Exception while creating StepRequest: " + e1);226throw new Failure("setStep(): A StepRequest has not been set up.");227}228229display("setStepRequest(): A StepRequest has been set up.");230return stepRequest;231}232233private void setAndCheckStepEvent ( BreakpointRequest bpRequest,234String caseProperty,235String threadName,236int testCaseIndex,237int stepDepth) {238display("Wait for brakepoint event in " + threadName);239BreakpointEvent bpEvent = (BreakpointEvent)waitForEvent(bpRequest);240241// check location of breakpoint event242int lineOfEvent = ((LocatableEvent)bpEvent).location().lineNumber();243if (lineOfEvent != lineForBreakInThread) {244complain("Wrong line number of BreakpointEvent for " + threadName);245complain("\texpected value : " + lineForBreakInThread + "; got one : " + lineOfEvent);246exitCode = FAILED;247}248249ThreadReference thread = debuggee.threadByNameOrThrow(threadName);250StepRequest stepRequest = setStepRequest( thread,251StepRequest.STEP_LINE,252stepDepth,253caseProperty);254stepRequest.enable();255256display("waiting for StepEvent in " + threadName);257Event newEvent = waitForEvent(stepRequest);258if (newEvent instanceof StepEvent) {259String property = (String) newEvent.request().getProperty("number");260display("got new StepEvent with property 'number' == " + property);261262if ( !property.equals(caseProperty) ) {263complain("property is not : " + caseProperty);264exitCode = FAILED;265}266// check location of step event267lineOfEvent = ((LocatableEvent)newEvent).location().lineNumber();268boolean isCorrectLine = lineOfEvent == checkedLines[testCaseIndex] || lineOfEvent == checkedLinesAlt[testCaseIndex];269if (!isCorrectLine) {270switch (stepDepth) {271case StepRequest.STEP_INTO:272complain("Wrong line number of StepEvent for request with depth == StepRequest.STEP_INTO:" );273break;274case StepRequest.STEP_OVER:275complain("Wrong line number of StepEvent for request with depth == StepRequest.STEP_OVER:" );276break;277case StepRequest.STEP_OUT:278complain("Wrong line number of StepEvent for request with depth == StepRequest.STEP_OUT:" );279break;280}281String msg = "\texpected line %d or %d; got %d";282complain(String.format(msg, checkedLines[testCaseIndex], checkedLinesAlt[testCaseIndex], lineOfEvent));283exitCode = FAILED;284}285286} else if (newEvent instanceof BreakpointEvent) {287vm.resume();288exitCode = FAILED;289complain("got unexpected BreakpointEvent, but StepEvent is not received");290} else if (newEvent instanceof VMDeathEvent) {291exitCode = FAILED;292throw new Failure("got unexpected VMDeathtEvent, but StepEvent is not received");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) {374375vm.resume();376Event resultEvent = null;377try {378eventSet = null;379eventIterator = null;380eventSet = vm.eventQueue().remove(waitTime);381if (eventSet == null) {382throw new Failure("TIMEOUT while waiting for an event");383}384eventIterator = eventSet.eventIterator();385while (eventIterator.hasNext()) {386Event curEvent = eventIterator.nextEvent();387if (curEvent instanceof VMDisconnectEvent) {388throw new Failure("Unexpected VMDisconnectEvent received.");389} else {390EventRequest evRequest = curEvent.request();391if (evRequest != null && evRequest.equals(eventRequest)) {392display("Requested event received: " + curEvent.toString() +393"; request property: " + (String) curEvent.request().getProperty("number"));394resultEvent = curEvent;395break;396} else {397throw new Failure("Unexpected event received: " + curEvent.toString());398}399}400}401} catch (Exception e) {402throw new Failure("Unexpected exception while waiting for an event: " + e);403}404return resultEvent;405}406407408private void getEventSet() {409try {410eventSet = vm.eventQueue().remove(waitTime);411if (eventSet == null) {412throw new Failure("TIMEOUT while waiting for an event");413}414eventIterator = eventSet.eventIterator();415} catch (Exception e) {416throw new Failure("getEventSet(): Unexpected exception while waiting for an event: " + e);417}418}419420private ReferenceType waitForDebuggeeClassPrepared () {421display("Creating request for ClassPrepareEvent for debuggee.");422ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();423cpRequest.addClassFilter(debuggeeName);424cpRequest.addCountFilter(1);425cpRequest.enable();426427ClassPrepareEvent event = (ClassPrepareEvent) waitForEvent(cpRequest);428cpRequest.disable();429430if (!event.referenceType().name().equals(debuggeeName)) {431throw new Failure("Unexpected class name for ClassPrepareEvent : " + debuggeeClass.name());432}433return event.referenceType();434}435436private int getInstruction () {437if (debuggeeClass == null) {438throw new Failure("getInstruction() :: debuggeeClass reference is null");439}440return ((IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();441}442443private void setInstruction (String instructionField) {444if (debuggeeClass == null) {445throw new Failure("getInstruction() :: debuggeeClass reference is null");446}447Field instrField = debuggeeClass.fieldByName("instruction");448IntegerValue instrValue = (IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName(instructionField)));449try {450((ClassType)debuggeeClass).setValue(instrField, instrValue );451} catch (InvalidTypeException e1) {452throw new Failure("Caught unexpected InvalidTypeException while setting value '" + instructionField + "' for instruction field");453} catch (ClassNotLoadedException e2) {454throw new Failure("Caught unexpected ClassNotLoadedException while setting value '" + instructionField + "' for instruction field");455}456}457}458459460