Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/BScenarios/hotswap/tc03x001.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: TC341* Description: After point of execution, different method42* Steps: 1.Set breakpoint at line 24 (call from a()43* to b())44* 2.Debug Main45* 3.Insert as first line in b():46* System.err.println("foo");47* 4.Smart Swap48* 5.Resume49* X. Prints out "foo" before 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* first line into <code>method_B</code> and resumes debugee. It is expected the59* new code will be actual before calling <code>method_C</code>60*/6162public class tc03x001 {6364public final static String UNEXPECTED_STRING = "***Unexpected exception ";6566private final static String prefix = "nsk.jdi.BScenarios.hotswap.";67private final static String debuggerName = prefix + "tc03x001";68private final static String debugeeName = debuggerName + "a";6970private final static String newClassFile = "newclass" + File.separator71+ debugeeName.replace('.',File.separatorChar)72+ ".class";7374private static int exitStatus;75private static Log log;76private static Debugee debugee;77private static long waitTime;78private static String classDir;7980private int expectedEventCount = 4;81private static final String methodNameToCheck = "method_C";8283private ReferenceType debugeeClass;8485private static void display(String msg) {86log.display(msg);87}8889private static void complain(String msg) {90log.complain("debugger FAILURE> " + msg + "\n");91}9293public static void main(String argv[]) {94System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));95}9697public static int run(String argv[], PrintStream out) {9899exitStatus = Consts.TEST_PASSED;100101tc03x001 thisTest = new tc03x001();102103ArgumentHandler argHandler = new ArgumentHandler(argv);104log = new Log(out, argHandler);105106classDir = argv[0];107waitTime = argHandler.getWaitTime() * 60000;108109Binder binder = new Binder(argHandler, log);110debugee = binder.bindToDebugee(debugeeName);111112try {113thisTest.execTest();114} catch (Throwable e) {115exitStatus = Consts.TEST_FAILED;116e.printStackTrace();117} finally {118debugee.endDebugee();119}120display("Test finished. exitStatus = " + exitStatus);121122return exitStatus;123}124125private void execTest() throws Failure {126127if (!debugee.VM().canRedefineClasses()) {128display("\n>>>canRedefineClasses() is false<<< test canceled.\n");129return;130}131132display("\nTEST BEGINS");133display("===========");134135EventSet eventSet = null;136EventIterator eventIterator = null;137Event event;138long totalTime = waitTime;139long tmp, begin = System.currentTimeMillis(),140delta = 0;141boolean exit = false;142143int eventCount = 0;144EventRequestManager evm = debugee.getEventRequestManager();145ClassPrepareRequest req = evm.createClassPrepareRequest();146req.addClassFilter(debugeeName);147req.enable();148debugee.resume();149150while (totalTime > 0 && !exit) {151if (eventIterator == null || !eventIterator.hasNext()) {152try {153eventSet = debugee.VM().eventQueue().remove(totalTime);154} catch (InterruptedException e) {155new Failure(e);156}157if (eventSet != null) {158eventIterator = eventSet.eventIterator();159} else {160eventIterator = null;161}162}163if (eventIterator != null) {164while (eventIterator.hasNext()) {165event = eventIterator.nextEvent();166// display("\n event ===>>> " + event);167168if (event instanceof ClassPrepareEvent) {169display("\n event ===>>> " + event);170debugeeClass = ((ClassPrepareEvent )event).referenceType();171display("Tested class\t:" + debugeeClass.name());172debugee.setBreakpoint(debugeeClass,173tc03x001a.brkpMethodName,174tc03x001a.brkpLineNumber);175176debugee.resume();177eventCount++;178179} else if (event instanceof BreakpointEvent) {180display("\n event ===>>> " + event);181display("redefining...");182redefineDebugee();183184createMethodEntryRequest(debugeeClass);185debugee.resume();186eventCount++;187188} else if (event instanceof MethodEntryEvent) {189display("\n event ===>>> " + event);190Method mthd = ((MethodEntryEvent )event).method();191eventCount++;192display("exiting from \"" + mthd.name() + "\" method");193if (mthd.name().compareTo(methodNameToCheck) == 0) {194Field fld = debugeeClass.fieldByName(tc03x001a.fieldToCheckName);195Value val = debugeeClass.getValue(fld);196if (((IntegerValue )val).value() == tc03x001a.CHANGED_VALUE) {197display("!!!Expected: new code is actual "198+ "before calling " + methodNameToCheck199+ "!!!");200} else {201complain("Unexpected: new code is not actual "202+ "before calling " + methodNameToCheck);203complain("Unexpected value of checked field: "204+ val);205exitStatus = Consts.TEST_FAILED;206}207}208debugee.resume();209210} else if (event instanceof VMDeathEvent) {211exit = true;212break;213} else if (event instanceof VMDisconnectEvent) {214exit = true;215break;216} // if217} // while218} // if219tmp = System.currentTimeMillis();220delta = tmp - begin;221totalTime -= delta;222begin = tmp;223}224225if (eventCount != expectedEventCount) {226if (totalTime <= 0) {227complain("out of wait time...");228}229complain("expecting " + expectedEventCount230+ " events, but "231+ eventCount + " events arrived.");232exitStatus = Consts.TEST_FAILED;233}234235display("=============");236display("TEST FINISHES\n");237}238239private void redefineDebugee() {240Map<com.sun.jdi.ReferenceType,byte[]> mapBytes;241boolean alreadyComplained = false;242mapBytes = mapClassToBytes(newClassFile);243try {244debugee.VM().redefineClasses(mapBytes);245} catch (Exception e) {246throw new Failure(UNEXPECTED_STRING + e);247}248}249250private Map<com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {251display("class-file\t:" + fileName);252File fileToBeRedefined = new File(classDir + File.separator + fileName);253int fileToRedefineLength = (int )fileToBeRedefined.length();254byte[] arrayToRedefine = new byte[fileToRedefineLength];255256FileInputStream inputFile;257try {258inputFile = new FileInputStream(fileToBeRedefined);259} catch (FileNotFoundException e) {260throw new Failure(UNEXPECTED_STRING + e);261}262263try {264inputFile.read(arrayToRedefine);265inputFile.close();266} catch (IOException e) {267throw new Failure(UNEXPECTED_STRING + e);268}269HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();270mapForClass.put(debugeeClass, arrayToRedefine);271return mapForClass;272}273274private MethodEntryRequest createMethodEntryRequest(ReferenceType refType) {275EventRequestManager evm = debugee.getEventRequestManager();276MethodEntryRequest request = evm.createMethodEntryRequest();277request.addClassFilter(refType);278request.enable();279return request;280}281}282283284