Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/BScenarios/hotswap/tc09x002.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.BScenarios.hotswap;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import com.sun.jdi.request.*;31import com.sun.jdi.event.*;3233import java.util.*;34import java.io.*;3536/**37* This test is from the group of so-called Borland's scenarios and38* implements the following test case: <br>39* Suite 3 - Hot Swap <br>40* Test case: TC9 <br>41* Description: Breakpoints updated correctly <br>42* Steps: 1.Set breakpoint at lines 36 and 39 <br>43* (printing 1 and 4) <br>44* 2.Debug Main <br>45* X. Stops on line 36 <br>46* 3.Delete line 37 <br>47* 4.Smart Swap <br>48* X. Breakpoints still set and valid at 36 <br>49* and 38 <br>50* 5.Resume <br>51* X. Stops on line 38 <br>52* <br>53* The description was drown up according to steps under JBuilder.54*55* Of course, the test has own line numbers and method/class names and56* works as follow:57* When the test is starting debugee, debugger sets breakpoints at58* the 47th and 49th line (method <code>method_C</code>).59* After the first breakpoint is reached, debugger redefines debugee60* deleting 48th line and attempts to set another breakpoint at 49th line.61* Because line number info is not available after redefining, it is62* expected IndexOutOfBoundsException will be thrown.63*/6465public class tc09x002 {6667public final static String UNEXPECTED_STRING = "***Unexpected exception ";6869private final static String prefix = "nsk.jdi.BScenarios.hotswap.";70private final static String debuggerName = prefix + "tc09x002";71private final static String debugeeName = debuggerName + "a";7273private final static String newClassFile = "newclass" + File.separator74+ debugeeName.replace('.',File.separatorChar)75+ ".class";7677private static int exitStatus;78private static Log log;79private static Debugee debugee;80private static long waitTime;81private static String classDir;8283private int eventCount;84private int expectedEventCount = 1;85private ReferenceType debugeeClass;8687private static void display(String msg) {88log.display(msg);89}9091private static void complain(String msg) {92log.complain("debugger FAILURE> " + msg + "\n");93}9495public static void main(String argv[]) {96System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));97}9899public static int run(String argv[], PrintStream out) {100101exitStatus = Consts.TEST_PASSED;102103tc09x002 thisTest = new tc09x002();104105ArgumentHandler argHandler = new ArgumentHandler(argv);106log = new Log(out, argHandler);107108classDir = argv[0];109waitTime = argHandler.getWaitTime() * 60000;110111Binder binder = new Binder(argHandler, log);112debugee = binder.bindToDebugee(debugeeName);113114try {115thisTest.execTest();116} catch (Throwable e) {117exitStatus = Consts.TEST_FAILED;118e.printStackTrace();119} finally {120debugee.endDebugee();121}122display("Test finished. exitStatus = " + exitStatus);123124return exitStatus;125}126127private void execTest() throws Failure {128129if (!debugee.VM().canRedefineClasses()) {130display("\n>>>canRedefineClasses() is false<<< test canceled.\n");131return;132}133134display("\nTEST BEGINS");135display("===========");136137EventSet eventSet = null;138EventIterator eventIterator = null;139Event event;140BreakpointRequest brkpRequest1, brkpRequest2;141long totalTime = waitTime;142long tmp, begin = System.currentTimeMillis(),143delta = 0;144boolean exit = false;145146eventCount = 0;147EventRequestManager evm = debugee.getEventRequestManager();148ClassPrepareRequest req = evm.createClassPrepareRequest();149req.addClassFilter(debugeeName);150req.enable();151debugee.resume();152153while (totalTime > 0 && !exit) {154if (eventIterator == null || !eventIterator.hasNext()) {155try {156eventSet = debugee.VM().eventQueue().remove(totalTime);157} catch (InterruptedException e) {158new Failure(e);159}160if (eventSet != null) {161eventIterator = eventSet.eventIterator();162} else {163eventIterator = null;164}165}166while (eventIterator.hasNext()) {167event = eventIterator.nextEvent();168// display("\n event ===>>> " + event);169170if (event instanceof ClassPrepareEvent) {171display("\n event ===>>> " + event);172debugeeClass = ((ClassPrepareEvent )event).referenceType();173display("Tested class\t:" + debugeeClass.name());174175brkpRequest1 = debugee.setBreakpoint(debugeeClass,176tc09x002a.brkpMethodName,177tc09x002a.brkpLineNumber1);178179brkpRequest2 = debugee.setBreakpoint(debugeeClass,180tc09x002a.brkpMethodName,181tc09x002a.brkpLineNumber2);182183breakpointInfo(brkpRequest1);184breakpointInfo(brkpRequest2);185186debugee.resume();187188} else if (event instanceof BreakpointEvent) {189display("\n event ===>>> " + event);190hitBreakpoint((BreakpointEvent )event);191192if (eventCount == 1) {193display("redefining...");194redefineDebugee();195Method method = null;196try {197method = ((BreakpointEvent )event).thread().frame(0).location().method();198} catch (IncompatibleThreadStateException e) {199throw new Failure(UNEXPECTED_STRING + e);200}201202display("\nsetting breakpoint at the redefined method...");203try {204Location location = debugee.getLineLocation(method, tc09x002a.brkpLineNumber2);205if (location != null) {206brkpRequest2 = evm.createBreakpointRequest(location);207if (method.isObsolete()) {208complain("Locations of the redefined method "209+ "should be not available: " + location);210complain("BreakpointRequest for not available location "211+ "should be created: " + brkpRequest2);212exitStatus = Consts.TEST_FAILED;213} else {214display("No location found for unavailable line after class redefinition");215}216}217} catch (Exception e) {218display("Unspecified exception caught while setting breakpoint "219+ "to unavailable line after class redefinition:\n\t" + e);220exitStatus = Consts.TEST_FAILED;221}222}223debugee.resume();224225} else if (event instanceof VMDeathEvent) {226exit = true;227break;228} else if (event instanceof VMDisconnectEvent) {229exit = true;230break;231} // if232} // while233tmp = System.currentTimeMillis();234delta = tmp - begin;235totalTime -= delta;236begin = tmp;237}238239if (eventCount != expectedEventCount) {240if (totalTime <= 0) {241complain("out of wait time...");242}243complain("expecting " + expectedEventCount244+ " events, but "245+ eventCount + " events arrived.");246exitStatus = Consts.TEST_FAILED;247}248249display("=============");250display("TEST FINISHES\n");251}252253private void redefineDebugee() {254Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;255boolean alreadyComplained = false;256mapBytes = mapClassToBytes(newClassFile);257try {258debugee.VM().redefineClasses(mapBytes);259} catch (Exception e) {260throw new Failure(UNEXPECTED_STRING + e);261}262}263264private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {265display("class-file\t:" + fileName);266File fileToBeRedefined = new File(classDir + File.separator + fileName);267int fileToRedefineLength = (int )fileToBeRedefined.length();268byte[] arrayToRedefine = new byte[fileToRedefineLength];269270FileInputStream inputFile;271try {272inputFile = new FileInputStream(fileToBeRedefined);273} catch (FileNotFoundException e) {274throw new Failure(UNEXPECTED_STRING + e);275}276277try {278inputFile.read(arrayToRedefine);279inputFile.close();280} catch (IOException e) {281throw new Failure(UNEXPECTED_STRING + e);282}283HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();284mapForClass.put(debugeeClass, arrayToRedefine);285return mapForClass;286}287288private void hitBreakpoint(BreakpointEvent event) {289eventInfo(event);290if (event.location().lineNumber() == tc09x002a.checkLastLine &&291eventCount == 1) {292display("!!!BreakpointEvent steps to the expected line "293+ event.location().lineNumber() + "!!!");294} else {295complain("BreakpointEvent steps to line " + event.location().lineNumber()296+ ", expected line number is "297+ tc09x002a.checkLastLine);298exitStatus = Consts.TEST_FAILED;299}300display("");301}302303private void eventInfo(LocatableEvent event) {304eventCount++;305display("event info: #" + eventCount);306display("\tthread\t- " + event.thread().name());307locationInfo(event);308}309310private void breakpointInfo(BreakpointRequest request) {311display("breakpoint info: ");312display("\tis enabled - " + request.isEnabled());313locationInfo(request);314}315316private void locationInfo(Locatable loc) {317try {318display("\tsource\t- " + loc.location().sourceName());319display("\tmethod\t- " + loc.location().method().name());320display("\tline\t- " + loc.location().lineNumber());321} catch (AbsentInformationException e) {322display("***information is not available***");323}324}325}326327328