Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/BScenarios/hotswap/tc05x001.java
41171 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* Working of debugger and debugee is synchronized via IOPipe channel.62*/6364public class tc05x001 {6566public final static String UNEXPECTED_STRING = "***Unexpected exception ";6768private final static String prefix = "nsk.jdi.BScenarios.hotswap.";69private final static String debuggerName = prefix + "tc05x001";70private final static String debugeeName = debuggerName + "a";7172public final static String PERFORM_SGL = "perform";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 testDir;8384private static final String firstMethodName = "method_B";8586private int eventCount;87private int expectedEventCount = 2;88private ClassType debugeeClass;89StepRequest stepRequest = null;9091private static void display(String msg) {92log.display(msg);93}9495private static void complain(String msg) {96log.complain("debugger FAILURE> " + msg + "\n");97}9899public static void main(String argv[]) {100System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));101}102103public static int run(String argv[], PrintStream out) {104105exitStatus = Consts.TEST_PASSED;106107tc05x001 thisTest = new tc05x001();108109ArgumentHandler argHandler = new ArgumentHandler(argv);110log = new Log(out, argHandler);111112testDir = argv[0];113waitTime = argHandler.getWaitTime() * 60000;114115debugee = Debugee.prepareDebugee(argHandler, log, debugeeName);116117try {118thisTest.execTest();119} catch (Throwable e) {120exitStatus = Consts.TEST_FAILED;121e.printStackTrace();122} finally {123debugee.resume();124debugee.quit();125}126display("Test finished. exitStatus = " + exitStatus);127128return exitStatus;129}130131private void execTest() throws Failure {132133if (!debugee.VM().canRedefineClasses()) {134display("\n>>>canRedefineClasses() is false<<< test canceled.\n");135return;136}137138debugeeClass = (ClassType)debugee.classByName(debugeeName);139140display("\nTEST BEGINS");141display("===========");142143debugee.VM().suspend();144145display("Tested class\t:" + debugeeClass.name());146147BreakpointRequest brkp = debugee.setBreakpoint(debugeeClass,148tc05x001a.brkpMethodName,149tc05x001a.brkpLineNumber);150debugee.resume();151debugee.sendSignal(PERFORM_SGL);152153BreakpointEvent brkpEvent = waitBreakpointEvent(brkp, waitTime);154155display("\nredefining...");156redefineDebugee();157158ThreadReference thread = brkpEvent.thread();159160EventRequestManager evm = debugee.getEventRequestManager();161162display("creating step request INTO...");163stepRequest = evm.createStepRequest(thread, StepRequest.STEP_LINE,164StepRequest.STEP_INTO);165stepRequest.addClassFilter(debugeeClass);166stepRequest.enable();167debugee.resume();168169boolean cmp = false;170StepEvent stepEvent;171Event event;172for (int i = 0; i < 3; i++) {173// waiting the breakpoint event174display("waiting step event...");175try {176event = debugee.waitingEvent(stepRequest, waitTime);177} catch (Exception e) {178stepRequest.disable();179throw new Failure(UNEXPECTED_STRING + e);180}181if (!(event instanceof StepEvent )) {182stepRequest.disable();183throw new Failure("StepEvent didn't arrive");184}185if (hitStep((StepEvent )event)) {186break;187}188debugee.resume();189}190evm.deleteEventRequest(stepRequest);191debugee.resume();192193display("=============");194display("TEST FINISHES\n");195}196197private void redefineDebugee() {198Map<com.sun.jdi.ReferenceType,byte[]> mapBytes;199boolean alreadyComplained = false;200mapBytes = mapClassToBytes(newClassFile);201try {202debugee.VM().redefineClasses(mapBytes);203} catch (Exception e) {204throw new Failure(UNEXPECTED_STRING + e);205}206}207208private Map<com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {209display("class-file\t:" + fileName);210File fileToBeRedefined = new File(testDir + File.separator + fileName);211int fileToRedefineLength = (int )fileToBeRedefined.length();212byte[] arrayToRedefine = new byte[fileToRedefineLength];213214FileInputStream inputFile;215try {216inputFile = new FileInputStream(fileToBeRedefined);217} catch (FileNotFoundException e) {218throw new Failure(UNEXPECTED_STRING + e);219}220221try {222inputFile.read(arrayToRedefine);223inputFile.close();224} catch (IOException e) {225throw new Failure(UNEXPECTED_STRING + e);226}227HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();228mapForClass.put(debugeeClass, arrayToRedefine);229return mapForClass;230}231232private BreakpointEvent waitBreakpointEvent(BreakpointRequest brkp, long waitTime) {233Event event;234try {235event = debugee.waitingEvent(brkp, waitTime);236} catch (InterruptedException e) {237throw new Failure(UNEXPECTED_STRING + e);238}239if (!(event instanceof BreakpointEvent )) {240throw new Failure("BreakpointEvent didn't arrive");241}242243return (BreakpointEvent )event;244}245246private boolean hitStep(StepEvent event) {247locationInfo(event);248String methodName = event.location().method().name();249boolean ret = methodName.compareTo(firstMethodName) != 0;250if (ret) {251if (!event.location().method().isObsolete()) {252complain("Unexpected event" + event);253exitStatus = Consts.TEST_FAILED;254} else {255display("!!!Expected event location at \"" + methodName + "\"");256}257} else {258display("!!!Expected event location at \"" + methodName + "\"");259stepRequest.disable();260}261display("");262return !ret;263}264265private void locationInfo(LocatableEvent event) {266eventCount++;267display("event info: #" + eventCount);268display("\tthread\t- " + event.thread().name());269try {270display("\tsource\t- " + event.location().sourceName());271display("\tmethod\t- " + event.location().method().name());272display("\tline\t- " + event.location().lineNumber());273} catch (AbsentInformationException e) {274}275}276}277278279