Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/createStepRequest/crstepreq007.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 crstepreq007 {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 = ".crstepreq007";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 = 4;78static final String[] brakeMethods = {79"caseRun",80"m00",81"caseRun",82"caseRun"83};84static final int[][] checkedLines = {85{ 152, 174, 152},86{ 174, 157, 178},87{ 161, 178, 174},88{ 165, 182, 185}89};9091static final String debuggeeThreadName = prefix + ".Thread0crstepreq007a";9293//------------------------------------------------------ mutable common methods9495public static int run (String argv[], PrintStream out) {9697int exitStatus = new crstepreq007().runThis(argv, out);98System.out.println (exitStatus == PASSED ? "TEST PASSED" : "TEST FAILED");99return exitCode;100}101102private int runThis(String argv[], PrintStream out) {103104argHandler = new ArgumentHandler(argv);105log = new Log(out, argHandler);106waitTime = argHandler.getWaitTime() * 60000;107108try {109110Binder binder = new Binder(argHandler, log);111debuggee = binder.bindToDebugee(debuggeeName);112debuggee.redirectStderr(log, "");113eventRManager = debuggee.getEventRequestManager();114115vm = debuggee.VM();116eventRManager = vm.eventRequestManager();117118debuggeeClass = waitForDebuggeeClassPrepared();119120execTest();121122debuggee.resume();123getEventSet();124if (eventIterator.nextEvent() instanceof VMDeathEvent) {125display("Waiting for the debuggee's finish...");126debuggee.waitFor();127128display("Getting the debuggee's exit status.");129int status = debuggee.getStatus();130if (status != (PASSED + PASS_BASE)) {131complain("Debuggee returned UNEXPECTED exit status: " + status);132exitCode = Consts.TEST_FAILED;133}134} else {135throw new TestBug("Last event is not the VMDeathEvent");136}137138} catch (VMDisconnectedException e) {139exitCode = Consts.TEST_FAILED;140complain("The test cancelled due to VMDisconnectedException.");141e.printStackTrace(out);142display("Trying: vm.process().destroy();");143if (vm != null) {144Process vmProcess = vm.process();145if (vmProcess != null) {146vmProcess.destroy();147}148}149150} catch (Exception e) {151exitCode = Consts.TEST_FAILED;152complain("Unexpected Exception: " + e.getMessage());153e.printStackTrace(out);154complain("The test has not finished normally. Forcing: vm.exit().");155if (vm != null) {156vm.exit(PASSED + PASS_BASE);157}158debuggee.resume();159getEventSet();160}161162return exitCode;163}164165//--------------------------------------------------------- mutable common methods166167private void execTest() {168BreakpointRequest bpRequest = setBreakpoint( null,169debuggeeClass,170"methodForCommunication",171lineForBreak,172"breakForCommunication");173bpRequest.enable();174175StepRequest stepRequest = null;176177display("TESTING BEGINS");178for (int testCase = 0; testCase < maxCase && instruction != quit; testCase++) {179180instruction = getInstruction();181if (instruction == quit) {182vm.resume();183break;184}185186display(":: CASE # " + testCase);187stepRequest = setStepRequest( bpRequest,188"thread" + testCase,189testCase,190"stepRequest" + testCase );191192checkStepEvent( stepRequest,193"thread" + testCase,194testCase );195}196display("TESTING ENDS");197}198199//--------------------------------------------------------- test specific methods200201private StepRequest setStepRequest ( BreakpointRequest bpRequest,202String threadName,203int testCase,204String property ) {205StepRequest stepRequest = null;206for (;;) {207display("Wait for initial brakepoint event in " + threadName);208BreakpointEvent bpEvent = (BreakpointEvent)waitForEvent(bpRequest);209210// check location of breakpoint event211int lineOfEvent = ((LocatableEvent)bpEvent).location().lineNumber();212if (lineOfEvent != lineForBreak) {213complain("Wrong line number of initial brakepoint event for " + threadName);214complain("\texpected value : " + lineForBreak + "; got one : " + lineOfEvent);215break;216}217218display("Getting mirror of thread: " + threadName);219ThreadReference thread = debuggee.threadByNameOrThrow(threadName);220221display("Getting ReferenceType of thread: " + threadName);222ReferenceType debuggeeThread = debuggee.classByName(debuggeeThreadName);223224// set second breakpoint to suspend checked thread at the right location before225// setting step request226BreakpointRequest bpRequest1 = setBreakpoint( thread,227debuggeeThread,228brakeMethods[testCase],229checkedLines[testCase][0],230"");231bpRequest1.addCountFilter(1);232bpRequest1.enable();233234display("Wait for additional brakepoint event in " + threadName);235bpEvent = (BreakpointEvent)waitForEvent(bpRequest1);236237// check location of breakpoint event238lineOfEvent = ((LocatableEvent)bpEvent).location().lineNumber();239if (lineOfEvent != checkedLines[testCase][0]) {240complain("Wrong line number of additional brakepoint event for " + threadName);241complain("\texpected value : " + checkedLines[testCase][0] + "; got one : " + lineOfEvent);242break;243}244245display("Setting a step request in thread: " + thread);246try {247stepRequest = eventRManager.createStepRequest ( thread,248StepRequest.STEP_LINE,249StepRequest.STEP_INTO );250stepRequest.putProperty("number", property);251} catch ( Exception e1 ) {252complain("setStepRequest(): unexpected Exception while creating StepRequest: " + e1);253break;254}255break;256}257if (stepRequest == null) {258throw new Failure("setStepRequest(): StepRequest has not been set up.");259}260display("setStepRequest(): StepRequest has been set up.");261return stepRequest;262}263264private void checkStepEvent ( StepRequest stepRequest,265String threadName,266int testCase ) {267stepRequest.enable();268269display("waiting for first StepEvent in " + threadName);270Event newEvent = waitForEvent(stepRequest);271display("got first StepEvent");272273display("CHECK1 for line location of first StepEvent.");274int lineOfEvent = ((LocatableEvent)newEvent).location().lineNumber();275if (lineOfEvent != checkedLines[testCase][1]) {276complain("CHECK1 for line location of first StepEvent FAILED for CASE # " + testCase);277complain("\texpected value : " + checkedLines[testCase][1] + "; got one : " + lineOfEvent);278exitCode = FAILED;279} else {280display("CHECK1 PASSED");281}282283display("waiting for second StepEvent in " + threadName);284newEvent = waitForEvent(stepRequest);285display("got second StepEvent");286287display("CHECK2 for line location of second StepEvent.");288lineOfEvent = ((LocatableEvent)newEvent).location().lineNumber();289if (lineOfEvent != checkedLines[testCase][2]) {290complain("CHECK2 for line location of second StepEvent FAILED for CASE # " + testCase);291complain("\texpected value : " + checkedLines[testCase][2] + "; got one : " + lineOfEvent);292exitCode = FAILED;293} else {294display("CHECK2 PASSED");295}296297stepRequest.disable();298eventRManager.deleteEventRequest(stepRequest);299stepRequest = null;300display("request for StepEvent in " + threadName + " is deleted");301}302303//--------------------------------------------------------- immutable common methods304305void display(String msg) {306log.display("debugger > " + msg);307}308309void complain(String msg) {310log.complain("debugger FAILURE > " + msg);311}312313/**314* Sets up a breakpoint at given line number within a given method in a given class315* for a given thread.316*317* Returns a BreakpointRequest object in case of success, otherwise throws Failure.318*/319private BreakpointRequest setBreakpoint ( ThreadReference thread,320ReferenceType testedClass,321String methodName,322int bpLine,323String property) {324325display("Setting a breakpoint in :");326display(" thread: " + thread + "; class: " + testedClass +327"; method: " + methodName + "; line: " + bpLine + "; property: " + property);328329List allLineLocations = null;330Location lineLocation = null;331BreakpointRequest breakpRequest = null;332333try {334Method method = (Method) testedClass.methodsByName(methodName).get(0);335336allLineLocations = method.allLineLocations();337338display("Getting location for breakpoint...");339Iterator locIterator = allLineLocations.iterator();340while (locIterator.hasNext()) {341Location curLocation = (Location)locIterator.next();342int curNumber = curLocation.lineNumber();343if (curLocation.lineNumber() == bpLine) {344lineLocation = curLocation;345break;346}347}348if (lineLocation == null) {349throw new TestBug("Incorrect line number of methods' location");350}351352try {353breakpRequest = eventRManager.createBreakpointRequest(lineLocation);354if (thread != null) {355breakpRequest.addThreadFilter(thread);356}357breakpRequest.putProperty("number", property);358} catch ( Exception e1 ) {359complain("setBreakpoint(): unexpected Exception while creating BreakpointRequest: " + e1);360breakpRequest = null;361}362} catch ( Exception e2 ) {363complain("setBreakpoint(): unexpected Exception while getting locations: " + e2);364breakpRequest = null;365}366367if (breakpRequest == null) {368throw new Failure("setBreakpoint(): A breakpoint has not been set up.");369}370371display("setBreakpoint(): A breakpoint has been set up.");372return breakpRequest;373}374375private Event waitForEvent (EventRequest eventRequest) {376vm.resume();377Event resultEvent = null;378try {379eventSet = null;380eventIterator = null;381eventSet = vm.eventQueue().remove(waitTime);382if (eventSet == null) {383throw new Failure("TIMEOUT while waiting for an event");384}385eventIterator = eventSet.eventIterator();386while (eventIterator.hasNext()) {387Event curEvent = eventIterator.nextEvent();388if (curEvent instanceof VMDisconnectEvent) {389throw new Failure("Unexpected VMDisconnectEvent received.");390} else {391EventRequest evRequest = curEvent.request();392if (evRequest != null && evRequest.equals(eventRequest)) {393display("Requested event received: " + curEvent.toString() +394"; request property: " + (String) curEvent.request().getProperty("number"));395resultEvent = curEvent;396break;397} else {398throw new Failure("Unexpected event received: " + curEvent.toString());399}400}401}402} catch (Exception e) {403throw new Failure("Unexpected exception while waiting for an event: " + e);404}405return resultEvent;406}407408private Event waitForEvent () {409vm.resume();410Event resultEvent = null;411try {412eventSet = null;413eventIterator = null;414eventSet = vm.eventQueue().remove(waitTime);415if (eventSet == null) {416throw new Failure("TIMEOUT while waiting for an event");417}418eventIterator = eventSet.eventIterator();419while (eventIterator.hasNext()) {420resultEvent = eventIterator.nextEvent();421if (resultEvent instanceof VMDisconnectEvent) {422throw new Failure("Unexpected VMDisconnectEvent received.");423}424}425} catch (Exception e) {426throw new Failure("Unexpected exception while waiting for an event: " + e);427}428return resultEvent;429}430431private void getEventSet() {432try {433eventSet = vm.eventQueue().remove(waitTime);434if (eventSet == null) {435throw new Failure("TIMEOUT while waiting for an event");436}437eventIterator = eventSet.eventIterator();438} catch (Exception e) {439throw new Failure("getEventSet(): Unexpected exception while waiting for an event: " + e);440}441}442443private ReferenceType waitForDebuggeeClassPrepared () {444display("Creating request for ClassPrepareEvent for debuggee.");445ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();446cpRequest.addClassFilter(debuggeeName);447cpRequest.addCountFilter(1);448cpRequest.enable();449450ClassPrepareEvent event = (ClassPrepareEvent) waitForEvent(cpRequest);451cpRequest.disable();452453if (!event.referenceType().name().equals(debuggeeName)) {454throw new Failure("Unexpected class name for ClassPrepareEvent : " + debuggeeClass.name());455}456return event.referenceType();457}458459private int getInstruction () {460if (debuggeeClass == null) {461throw new Failure("getInstruction() :: debuggeeClass reference is null");462}463return ((IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();464}465466private void setInstruction (String instructionField) {467if (debuggeeClass == null) {468throw new Failure("getInstruction() :: debuggeeClass reference is null");469}470Field instrField = debuggeeClass.fieldByName("instruction");471IntegerValue instrValue = (IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName(instructionField)));472try {473((ClassType)debuggeeClass).setValue(instrField, instrValue );474} catch (InvalidTypeException e1) {475throw new Failure("Caught unexpected InvalidTypeException while setting value '" + instructionField + "' for instruction field");476} catch (ClassNotLoadedException e2) {477throw new Failure("Caught unexpected ClassNotLoadedException while setting value '" + instructionField + "' for instruction field");478}479}480}481482483