Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/BScenarios/hotswap/tc05x002.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: TC5 <br>41* Description: After point of execution, same method - stepping <br>42* Steps: 1.Set breakpoint at line 24 (call to b() <br>43* from a()) <br>44* 2.Debug Main <br>45* 3.Insert as next line after point of <br>46* execution: System.err.println("foo"); <br>47* 4.Smart Swap <br>48* 5.F7 to step into <br>49* X. Steps into method b() <br>50* <br>51* 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 38th line (method <code>method_A</code>).57* After the breakpoint is reached, debugger redefines debugee adding58* a new line into <code>method_A</code>, creates <code>StepRequest</code> and59* resumes debugee. When the location of the current <code>StepEvent</code> is60* in <code>method_B</code>, created <code>StepRequest</code> is disabled.61*/6263public class tc05x002 {6465public final static String UNEXPECTED_STRING = "***Unexpected exception ";6667private final static String prefix = "nsk.jdi.BScenarios.hotswap.";68private final static String debuggerName = prefix + "tc05x002";69private final static String debugeeName = debuggerName + "a";7071private final static String newClassFile = "newclass" + File.separator72+ debugeeName.replace('.',File.separatorChar)73+ ".class";7475private static int exitStatus;76private static Log log;77private static Debugee debugee;78private static long waitTime;79private static String classDir;8081private static final String firstMethodName = "method_B";8283private int eventCount;84private int expectedEventCount = 2;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;102103tc05x002 thisTest = new tc05x002();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;140long totalTime = waitTime;141long tmp, begin = System.currentTimeMillis(),142delta = 0;143boolean exit = false;144145eventCount = 0;146EventRequestManager evm = debugee.getEventRequestManager();147ClassPrepareRequest req = evm.createClassPrepareRequest();148req.addClassFilter(debugeeName);149req.enable();150debugee.resume();151152while (totalTime > 0 && !exit) {153if (eventIterator == null || !eventIterator.hasNext()) {154try {155eventSet = debugee.VM().eventQueue().remove(totalTime);156} catch (InterruptedException e) {157new Failure(e);158}159if (eventSet != null) {160eventIterator = eventSet.eventIterator();161} else {162eventIterator = null;163}164}165if (eventIterator != null) {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());174debugee.setBreakpoint(debugeeClass,175tc05x002a.brkpMethodName,176tc05x002a.brkpLineNumber);177178debugee.resume();179180} else if (event instanceof BreakpointEvent) {181display("\n event ===>>> " + event);182hitBreakpoint((BreakpointEvent )event);183display("redefining...");184redefineDebugee();185createStepRequest(((LocatableEvent )event).thread());186debugee.resume();187188} else if (event instanceof StepEvent) {189display("\n event ===>>> " + event);190hitStep((StepEvent )event);191debugee.resume();192193} else if (event instanceof VMDeathEvent) {194exit = true;195break;196} else if (event instanceof VMDisconnectEvent) {197exit = true;198break;199} // if200} // while201} // if202tmp = System.currentTimeMillis();203delta = tmp - begin;204totalTime -= delta;205begin = tmp;206}207208if (eventCount != expectedEventCount) {209if (totalTime <= 0) {210complain("out of wait time...");211}212complain("expecting " + expectedEventCount213+ " events, but "214+ eventCount + " events arrived.");215exitStatus = Consts.TEST_FAILED;216}217218display("=============");219display("TEST FINISHES\n");220}221222private void redefineDebugee() {223Map<com.sun.jdi.ReferenceType,byte[]> mapBytes;224boolean alreadyComplained = false;225mapBytes = mapClassToBytes(newClassFile);226try {227debugee.VM().redefineClasses(mapBytes);228} catch (Exception e) {229throw new Failure(UNEXPECTED_STRING + e);230}231}232233private Map<com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {234display("class-file\t:" + fileName);235File fileToBeRedefined = new File(classDir + File.separator + fileName);236int fileToRedefineLength = (int )fileToBeRedefined.length();237byte[] arrayToRedefine = new byte[fileToRedefineLength];238239FileInputStream inputFile;240try {241inputFile = new FileInputStream(fileToBeRedefined);242} catch (FileNotFoundException e) {243throw new Failure(UNEXPECTED_STRING + e);244}245246try {247inputFile.read(arrayToRedefine);248inputFile.close();249} catch (IOException e) {250throw new Failure(UNEXPECTED_STRING + e);251}252HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();253mapForClass.put(debugeeClass, arrayToRedefine);254return mapForClass;255}256257private StepRequest createStepRequest(ThreadReference thread) {258EventRequestManager evm = debugee.getEventRequestManager();259StepRequest request = evm.createStepRequest(thread,260StepRequest.STEP_LINE,261StepRequest.STEP_INTO);262request.enable();263return request;264}265266private void hitBreakpoint(BreakpointEvent event) {267locationInfo(event);268if (event.location().lineNumber() != tc05x002a.checkLastLine) {269complain("BreakpointEvent steps to line " + event.location().lineNumber()270+ ", expected line number is "271+ tc05x002a.checkLastLine);272exitStatus = Consts.TEST_FAILED;273} else {274display("!!!BreakpointEvent steps to the expected line "275+ event.location().lineNumber() + "!!!");276}277display("");278}279280private void hitStep(StepEvent event) {281locationInfo(event);282String methodName = event.location().method().name();283StepRequest request = (StepRequest )event.request();284if (methodName.compareTo(firstMethodName) != 0) {285if (!event.location().method().isObsolete()) {286complain("Unexpected event" + event);287exitStatus = Consts.TEST_FAILED;288} else {289display("!!!Expected step event!!!");290}291} else {292display("!!!Expected step event!!!");293request.disable();294}295display("");296}297298private void locationInfo(LocatableEvent event) {299if (!event.location().method().isObsolete()) {300eventCount++;301}302String methodName = "<>";303display("event info: #" + eventCount);304display("\tthread\t- " + event.thread().name());305try {306methodName = event.location().method().name();307display("\tsource\t- " + event.location().sourceName());308display("\tmethod\t- " + methodName);309display("\tline\t- " + event.location().lineNumber());310} catch (AbsentInformationException e) {311}312if (event.location().method().isObsolete()) {313display(methodName + " method is skipped");314}315}316}317318319