Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/BScenarios/hotswap/tc01x002.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:39* Suite 3 - Hot Swap40* Test case: TC141* Description: After point of execution, same method42* Steps: 1.Set breakpoint at line 24 (call to b()43* from a())44* 2.Debug Main45* 3.Insert as next line after point of46* execution: System.err.println("foo");47* 4.Smart Swap48* 5.Resume49* X. Prints out "foo" after printing the50* numbers51* The description was drown up according to steps under JBuilder.52*53* Of course, the test has own line numbers and method/class names and54* works as follow:55* When the test is starting debugee, debugger sets breakpoint at56* the 37th line (method <code>method_A</code>).57* After the breakpoint is reached, debugger redefines debugee adding58* a new line into <code>method_A</code>, pops current frame and resumes debugee.59* It is expected the new code will be actual after the redefining60*61* Step 5 of Borland's scenario is wrong due to redefineClasses spec says:62* "The redefined method will be used on new invokes. If resetting these63* frames is desired, use <code>ThreadReference.popFrames()</code> with64* <code>Method.isObsolete()</code>."65*/6667public class tc01x002 {6869public final static String UNEXPECTED_STRING = "***Unexpected exception ";7071private final static String prefix = "nsk.jdi.BScenarios.hotswap.";72private final static String debuggerName = prefix + "tc01x002";73private final static String debugeeName = debuggerName + "a";7475private final static String newClassFile = "newclass" + File.separator76+ debugeeName.replace('.',File.separatorChar)77+ ".class";7879private static int exitStatus;80private static Log log;81private static Debugee debugee;82private static long waitTime;83private static String classDir;8485private int expectedEventCount = 5;8687private ReferenceType debugeeClass;8889private static void display(String msg) {90log.display(msg);91}9293private static void complain(String msg) {94log.complain("debugger FAILURE> " + msg + "\n");95}9697public static void main(String argv[]) {98System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));99}100101public static int run(String argv[], PrintStream out) {102103exitStatus = Consts.TEST_PASSED;104105tc01x002 thisTest = new tc01x002();106107ArgumentHandler argHandler = new ArgumentHandler(argv);108log = new Log(out, argHandler);109110classDir = argv[0];111waitTime = argHandler.getWaitTime() * 60000;112113Binder binder = new Binder(argHandler, log);114debugee = binder.bindToDebugee(debugeeName);115116try {117thisTest.execTest();118} catch (Throwable e) {119exitStatus = Consts.TEST_FAILED;120e.printStackTrace();121} finally {122debugee.endDebugee();123}124display("Test finished. exitStatus = " + exitStatus);125126return exitStatus;127}128129private void execTest() throws Failure {130131if (!debugee.VM().canRedefineClasses()) {132display("\n>>>canRedefineClasses() is false<<< test canceled.\n");133return;134}135136display("\nTEST BEGINS");137display("===========");138139EventSet eventSet = null;140EventIterator eventIterator = null;141Event event;142long totalTime = waitTime;143long tmp, begin = System.currentTimeMillis(),144delta = 0;145boolean exit = false;146147int eventCount = 0;148EventRequestManager evm = debugee.getEventRequestManager();149ClassPrepareRequest req = evm.createClassPrepareRequest();150req.addClassFilter(debugeeName);151req.enable();152debugee.resume();153154while (totalTime > 0 && !exit) {155if (eventIterator == null || !eventIterator.hasNext()) {156try {157eventSet = debugee.VM().eventQueue().remove(totalTime);158} catch (InterruptedException e) {159new Failure(e);160}161if (eventSet != null) {162eventIterator = eventSet.eventIterator();163} else {164eventIterator = null;165}166}167if (eventIterator != null) {168while (eventIterator.hasNext()) {169event = eventIterator.nextEvent();170// display("\n event ===>>> " + event);171172if (event instanceof ClassPrepareEvent) {173display("\n event ===>>> " + event);174debugeeClass = ((ClassPrepareEvent )event).referenceType();175display("Tested class\t:" + debugeeClass.name());176debugee.setBreakpoint(debugeeClass,177tc01x002a.brkpMethodName,178tc01x002a.brkpLineNumber);179180debugee.resume();181eventCount++;182183} else if (event instanceof BreakpointEvent) {184display("\n event ===>>> " + event);185display("redefining...");186redefineDebugee();187popFrames(((BreakpointEvent )event).thread());188189createMethodExitRequest(debugeeClass);190debugee.resume();191eventCount++;192193} else if (event instanceof MethodExitEvent) {194display("\n event ===>>> " + event);195Method mthd = ((MethodExitEvent )event).method();196eventCount++;197display("exiting from \"" + mthd.name() + "\" method");198if (mthd.name().compareTo(tc01x002a.brkpMethodName) == 0) {199Field fld = debugeeClass.fieldByName(tc01x002a.fieldToCheckName);200Value val = debugeeClass.getValue(fld);201if (((IntegerValue )val).value() != tc01x002a.CHANGED_VALUE) {202complain("Unexpected: new code is not actual "203+ "after resetting frames");204complain("Unexpected value of checked field: "205+ val);206exitStatus = Consts.TEST_FAILED;207} else {208display("!!!Expected: new code is actual "209+ "after resetting frames!!!");210}211}212debugee.resume();213214} else if (event instanceof VMDeathEvent) {215exit = true;216break;217} else if (event instanceof VMDisconnectEvent) {218exit = true;219break;220} // if221} // while222} // if223tmp = System.currentTimeMillis();224delta = tmp - begin;225totalTime -= delta;226begin = tmp;227}228229if (eventCount != expectedEventCount) {230if (totalTime <= 0) {231complain("out of wait time...");232}233complain("expecting " + expectedEventCount234+ " events, but "235+ eventCount + " events arrived.");236exitStatus = Consts.TEST_FAILED;237}238239display("=============");240display("TEST FINISHES\n");241}242243private void redefineDebugee() {244Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;245boolean alreadyComplained = false;246mapBytes = mapClassToBytes(newClassFile);247try {248debugee.VM().redefineClasses(mapBytes);249} catch (Exception e) {250throw new Failure(UNEXPECTED_STRING + e);251}252}253254private void popFrames(ThreadReference thread) {255try {256StackFrame frame = thread.frame(0);257thread.popFrames(frame);258} catch (IncompatibleThreadStateException e) {259throw new Failure(UNEXPECTED_STRING + e);260}261}262263private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {264display("class-file\t:" + fileName);265File fileToBeRedefined = new File(classDir + File.separator + fileName);266int fileToRedefineLength = (int )fileToBeRedefined.length();267byte[] arrayToRedefine = new byte[fileToRedefineLength];268269FileInputStream inputFile;270try {271inputFile = new FileInputStream(fileToBeRedefined);272} catch (FileNotFoundException e) {273throw new Failure(UNEXPECTED_STRING + e);274}275276try {277inputFile.read(arrayToRedefine);278inputFile.close();279} catch (IOException e) {280throw new Failure(UNEXPECTED_STRING + e);281}282HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();283mapForClass.put(debugeeClass, arrayToRedefine);284return mapForClass;285}286287private MethodExitRequest createMethodExitRequest(ReferenceType refType) {288EventRequestManager evm = debugee.getEventRequestManager();289MethodExitRequest request = evm.createMethodExitRequest();290request.addClassFilter(refType);291request.enable();292return request;293}294}295296297