Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/BScenarios/hotswap/tc01x001.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>ethod_A</code> and resumes debugee. It is expected59* the added line will be not actual after the redefining according to60* redefineClasses spec:61* "The redefined method will be used on new invokes. If resetting these62* frames is desired, use <code>ThreadReference.popFrames()</code> with63* <code>Method.isObsolete()</code>."64*/6566public class tc01x001 {6768public final static String UNEXPECTED_STRING = "***Unexpected exception ";6970private final static String prefix = "nsk.jdi.BScenarios.hotswap.";71private final static String debuggerName = prefix + "tc01x001";72private final static String debugeeName = debuggerName + "a";7374private final static String newClassFile = "newclass" + File.separator75+ debugeeName.replace('.',File.separatorChar)76+ ".class";7778private static int exitStatus;79private static Log log;80private static Debugee debugee;81private static long waitTime;82private static String classDir;8384private int expectedEventCount = 5;8586private ReferenceType debugeeClass;8788private static void display(String msg) {89log.display(msg);90}9192private static void complain(String msg) {93log.complain("debugger FAILURE> " + msg + "\n");94}9596public static void main(String argv[]) {97System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));98}99100public static int run(String argv[], PrintStream out) {101102exitStatus = Consts.TEST_PASSED;103104tc01x001 thisTest = new tc01x001();105106ArgumentHandler argHandler = new ArgumentHandler(argv);107log = new Log(out, argHandler);108109classDir = argv[0];110waitTime = argHandler.getWaitTime() * 60000;111112Binder binder = new Binder(argHandler, log);113debugee = binder.bindToDebugee(debugeeName);114115try {116thisTest.execTest();117} catch (Throwable e) {118exitStatus = Consts.TEST_FAILED;119e.printStackTrace();120} finally {121debugee.endDebugee();122}123display("Test finished. exitStatus = " + exitStatus);124125return exitStatus;126}127128private void execTest() throws Failure {129130if (!debugee.VM().canRedefineClasses()) {131display("\n>>>canRedefineClasses() is false<<< test canceled.\n");132return;133}134135display("\nTEST BEGINS");136display("===========");137138EventSet eventSet = null;139EventIterator eventIterator = null;140Event event;141long totalTime = waitTime;142long tmp, begin = System.currentTimeMillis(),143delta = 0;144boolean exit = false;145146int eventCount = 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}166if (eventIterator != null) {167while (eventIterator.hasNext()) {168event = eventIterator.nextEvent();169// display("\n event ===>>> " + event);170171if (event instanceof ClassPrepareEvent) {172display("\n event ===>>> " + event);173debugeeClass = ((ClassPrepareEvent )event).referenceType();174display("Tested class\t:" + debugeeClass.name());175debugee.setBreakpoint(debugeeClass,176tc01x001a.brkpMethodName,177tc01x001a.brkpLineNumber);178179debugee.resume();180eventCount++;181182} else if (event instanceof BreakpointEvent) {183display("\n event ===>>> " + event);184display("redefining...");185redefineDebugee();186187createMethodExitRequest(debugeeClass);188debugee.resume();189eventCount++;190191} else if (event instanceof MethodExitEvent) {192display("\n event ===>>> " + event);193Method mthd = ((MethodExitEvent )event).method();194eventCount++;195display("exiting from \"" + mthd.name() + "\" method");196if (mthd.isObsolete()) {197Field fld = debugeeClass.fieldByName(tc01x001a.fieldToCheckName);198Value val = debugeeClass.getValue(fld);199if (((IntegerValue )val).value() == tc01x001a.CHANGED_VALUE) {200complain("Unexpected: new code is actual "201+ "without resetting frames");202complain("Unexpected value of checked field: "203+ val);204exitStatus = Consts.TEST_FAILED;205} else {206display("!!!Expected: new code is not actual "207+ "without resetting frames!!!");208}209}210debugee.resume();211212} else if (event instanceof VMDeathEvent) {213exit = true;214break;215} else if (event instanceof VMDisconnectEvent) {216exit = true;217break;218} // if219} // while220} // if221tmp = System.currentTimeMillis();222delta = tmp - begin;223totalTime -= delta;224begin = tmp;225}226227if (eventCount != expectedEventCount) {228if (totalTime <= 0) {229complain("out of wait time...");230}231complain("expecting " + expectedEventCount232+ " events, but "233+ eventCount + " events arrived.");234exitStatus = Consts.TEST_FAILED;235}236237display("=============");238display("TEST FINISHES\n");239}240241private void redefineDebugee() {242Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;243boolean alreadyComplained = false;244mapBytes = mapClassToBytes(newClassFile);245try {246debugee.VM().redefineClasses(mapBytes);247} catch (Exception e) {248throw new Failure(UNEXPECTED_STRING + e);249}250}251252private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {253display("class-file\t:" + fileName);254File fileToBeRedefined = new File(classDir + File.separator + fileName);255int fileToRedefineLength = (int )fileToBeRedefined.length();256byte[] arrayToRedefine = new byte[fileToRedefineLength];257258FileInputStream inputFile;259try {260inputFile = new FileInputStream(fileToBeRedefined);261} catch (FileNotFoundException e) {262throw new Failure(UNEXPECTED_STRING + e);263}264265try {266inputFile.read(arrayToRedefine);267inputFile.close();268} catch (IOException e) {269throw new Failure(UNEXPECTED_STRING + e);270}271HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();272mapForClass.put(debugeeClass, arrayToRedefine);273return mapForClass;274}275276private MethodExitRequest createMethodExitRequest(ReferenceType refType) {277EventRequestManager evm = debugee.getEventRequestManager();278MethodExitRequest request = evm.createMethodExitRequest();279request.addClassFilter(refType);280request.enable();281return request;282}283}284285286