Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/BScenarios/hotswap/tc09x001.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: TC9 <br>41* Description: Breakpoints updated correctly <br>42* Steps: 1.Set breakpoint at lines 36 and 39 <br>43* (printing 1 and 4) <br>44* 2.Debug Main <br>45* X. Stops on line 36 <br>46* 3.Delete line 37 <br>47* 4.Smart Swap <br>48* X. Breakpoints still set and valid at 36 <br>49* and 38 <br>50* 5.Resume <br>51* X. Stops on line 38 <br>52* <br>53* The description was drown up according to steps under JBuilder.54*55* Of course, the test has own line numbers and method/class names and56* works as follow:57* When the test is starting debugee, debugger sets breakpoints at58* the 47th and 49th line (method <code>method_C</code>).59* After the first breakpoint is reached, debugger redefines debugee60* deleting 48th line and resumes debugee. No breakpoints are61* expected anymore.62*/6364public class tc09x001 {6566public final static String UNEXPECTED_STRING = "***Unexpected exception ";6768private final static String prefix = "nsk.jdi.BScenarios.hotswap.";69private final static String debuggerName = prefix + "tc09x001";70private final static String debugeeName = debuggerName + "a";7172private final static String newClassFile = "newclass" + File.separator73+ debugeeName.replace('.',File.separatorChar)74+ ".class";7576private static int exitStatus;77private static Log log;78private static Debugee debugee;79private static long waitTime;80private static String classDir;8182private int eventCount;83private int expectedEventCount = 1;84private ReferenceType debugeeClass;85private BreakpointRequest brkpRequest1 = null,86brkpRequest2 = null;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;103104tc09x001 thisTest = new tc09x001();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;145146eventCount = 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());175176brkpRequest1 = debugee.setBreakpoint(debugeeClass,177tc09x001a.brkpMethodName,178tc09x001a.brkpLineNumber1);179180brkpRequest2 = debugee.setBreakpoint(debugeeClass,181tc09x001a.brkpMethodName,182tc09x001a.brkpLineNumber2);183184breakpointInfo(brkpRequest1);185breakpointInfo(brkpRequest2);186187debugee.resume();188189} else if (event instanceof BreakpointEvent) {190display("\n event ===>>> " + event);191hitBreakpoint((BreakpointEvent )event);192if (eventCount == 1) {193display("redefining...");194redefineDebugee();195breakpointInfo(brkpRequest1);196breakpointInfo(brkpRequest2);197}198debugee.resume();199200} else if (event instanceof VMDeathEvent) {201exit = true;202break;203} else if (event instanceof VMDisconnectEvent) {204exit = true;205break;206} // if207} // while208} // if209tmp = System.currentTimeMillis();210delta = tmp - begin;211totalTime -= delta;212begin = tmp;213}214215if (eventCount != expectedEventCount) {216if (totalTime <= 0) {217complain("out of wait time...");218}219complain("expecting " + expectedEventCount220+ " events, but "221+ eventCount + " events arrived.");222exitStatus = Consts.TEST_FAILED;223}224225display("=============");226display("TEST FINISHES\n");227}228229private void redefineDebugee() {230Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;231boolean alreadyComplained = false;232mapBytes = mapClassToBytes(newClassFile);233try {234debugee.VM().redefineClasses(mapBytes);235} catch (Exception e) {236throw new Failure(UNEXPECTED_STRING + e);237}238}239240private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {241display("class-file\t:" + fileName);242File fileToBeRedefined = new File(classDir + File.separator + fileName);243int fileToRedefineLength = (int )fileToBeRedefined.length();244byte[] arrayToRedefine = new byte[fileToRedefineLength];245246FileInputStream inputFile;247try {248inputFile = new FileInputStream(fileToBeRedefined);249} catch (FileNotFoundException e) {250throw new Failure(UNEXPECTED_STRING + e);251}252253try {254inputFile.read(arrayToRedefine);255inputFile.close();256} catch (IOException e) {257throw new Failure(UNEXPECTED_STRING + e);258}259HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();260mapForClass.put(debugeeClass, arrayToRedefine);261return mapForClass;262}263264private void hitBreakpoint(BreakpointEvent event) {265eventInfo(event);266if (event.location().lineNumber() == tc09x001a.checkLastLine &&267eventCount == 1) {268display("!!!BreakpointEvent steps to the expected line "269+ event.location().lineNumber() + "!!!");270} else {271complain("BreakpointEvent steps to line " + event.location().lineNumber()272+ ", expected line number is "273+ tc09x001a.checkLastLine);274exitStatus = Consts.TEST_FAILED;275}276display("");277}278279private void eventInfo(LocatableEvent event) {280eventCount++;281display("event info: #" + eventCount);282display("\tthread\t- " + event.thread().name());283locationInfo(event);284}285286private void breakpointInfo(BreakpointRequest request) {287display("breakpoint info: ");288display("\tis enabled - " + request.isEnabled());289locationInfo(request);290}291292private void locationInfo(Locatable loc) {293try {294display("\tsource\t- " + loc.location().sourceName());295display("\tmethod\t- " + loc.location().method().name());296display("\tline\t- " + loc.location().lineNumber());297} catch (AbsentInformationException e) {298display("***information is not available***");299}300}301}302303304