Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/BScenarios/hotswap/tc08x001.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: TC8 <br>41* Description: Before point of execution, different method - stepping<br>42* Steps: 1.Set breakpoint at line 30 (call to c() <br>43* from b()) <br>44* 2.Debug Main <br>45* 3.Insert as first line in a(): <br>46* System.err.println("foo"); <br>47* 4.Smart Swap <br>48* 5.Set Smart PopFrame to beginging of <br>49* method a() <br>50* 6.F7 to step into <br>51* X. Steps into a() <br>52* 7.F7 to step into <br>53* X. Prints "foo" <br>54* <br>55* The description was drown up according to steps under JBuilder. <br>56* <br>57* Of course, the test has own line numbers and method/class names and <br>58* works as follow:59* When the test is starting debugee, debugger sets breakpoint at60* the 38th line (method <code>method_A</code>).61* After the breakpoint is reached, debugger redefines debugee adding62* a new line into <code>method_A</code>, pops frame, creates <code>StepRequest</code>63* and resumes debugee. When the second <code>StepEvent</code> is reached, created64* <code>StepRequest</code> is disabled.65*/6667public class tc08x001 {6869public final static String UNEXPECTED_STRING = "***Unexpected exception ";7071private final static String prefix = "nsk.jdi.BScenarios.hotswap.";72private final static String debuggerName = prefix + "tc08x001";73private final static String debugeeName = debuggerName + "a";7475private final static String newClassFile = "newclass" + File.separator76+ debugeeName.replace('.',File.separatorChar)77+ ".class";7879private static int exitStatus;80private static Log log;81private static Debugee debugee;82private static long waitTime;83private static String classDir;8485private static final String firstMethodName = "method_A";8687private int eventCount;88private int expectedEventCount = 3;89private ReferenceType debugeeClass;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;106107tc08x001 thisTest = new tc08x001();108109ArgumentHandler argHandler = new ArgumentHandler(argv);110log = new Log(out, argHandler);111112classDir = argv[0];113waitTime = argHandler.getWaitTime() * 60000;114115Binder binder = new Binder(argHandler, log);116debugee = binder.bindToDebugee(debugeeName);117118try {119thisTest.execTest();120} catch (Throwable e) {121exitStatus = Consts.TEST_FAILED;122e.printStackTrace();123} finally {124debugee.endDebugee();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}137138display("\nTEST BEGINS");139display("===========");140141EventSet eventSet = null;142EventIterator eventIterator = null;143Event event;144long totalTime = waitTime;145long tmp, begin = System.currentTimeMillis(),146delta = 0;147boolean exit = false;148149eventCount = 0;150EventRequestManager evm = debugee.getEventRequestManager();151ClassPrepareRequest req = evm.createClassPrepareRequest();152req.addClassFilter(debugeeName);153req.enable();154debugee.resume();155156while (totalTime > 0 && !exit) {157if (eventIterator == null || !eventIterator.hasNext()) {158try {159eventSet = debugee.VM().eventQueue().remove(totalTime);160} catch (InterruptedException e) {161new Failure(e);162}163if (eventSet != null) {164eventIterator = eventSet.eventIterator();165} else {166eventIterator = null;167}168}169if (eventIterator != null) {170while (eventIterator.hasNext()) {171event = eventIterator.nextEvent();172// display("\n event ===>>> " + event);173174if (event instanceof ClassPrepareEvent) {175display("\n event ===>>> " + event);176debugeeClass = ((ClassPrepareEvent )event).referenceType();177display("Tested class\t:" + debugeeClass.name());178debugee.setBreakpoint(debugeeClass,179tc08x001a.brkpMethodName,180tc08x001a.brkpLineNumber);181182debugee.resume();183184} else if (event instanceof BreakpointEvent) {185display("\n event ===>>> " + event);186hitBreakpoint((BreakpointEvent )event);187display("redefining...");188redefineDebugee();189popFrames(((BreakpointEvent )event).thread());190createStepRequest(((LocatableEvent )event).thread());191debugee.resume();192193} else if (event instanceof StepEvent) {194display("\n event ===>>> " + event);195hitStep((StepEvent )event);196debugee.resume();197198} else if (event instanceof VMDeathEvent) {199exit = true;200break;201} else if (event instanceof VMDisconnectEvent) {202exit = true;203break;204} // if205} // while206} // if207tmp = System.currentTimeMillis();208delta = tmp - begin;209totalTime -= delta;210begin = tmp;211}212213if (eventCount != expectedEventCount) {214if (totalTime <= 0) {215complain("out of wait time...");216}217complain("expecting " + expectedEventCount218+ " events, but "219+ eventCount + " events arrived.");220exitStatus = Consts.TEST_FAILED;221}222223display("=============");224display("TEST FINISHES\n");225}226227private void redefineDebugee() {228Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;229boolean alreadyComplained = false;230mapBytes = mapClassToBytes(newClassFile);231try {232debugee.VM().redefineClasses(mapBytes);233} catch (Exception e) {234throw new Failure(UNEXPECTED_STRING + e);235}236}237238private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {239display("class-file\t:" + fileName);240File fileToBeRedefined = new File(classDir + File.separator + fileName);241int fileToRedefineLength = (int )fileToBeRedefined.length();242byte[] arrayToRedefine = new byte[fileToRedefineLength];243244FileInputStream inputFile;245try {246inputFile = new FileInputStream(fileToBeRedefined);247} catch (FileNotFoundException e) {248throw new Failure(UNEXPECTED_STRING + e);249}250251try {252inputFile.read(arrayToRedefine);253inputFile.close();254} catch (IOException e) {255throw new Failure(UNEXPECTED_STRING + e);256}257HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();258mapForClass.put(debugeeClass, arrayToRedefine);259return mapForClass;260}261262private void popFrames(ThreadReference thread) {263display("\npop frames...");264try {265StackFrame frame = thread.frame(0);266Method mthd = frame.location().method();267do {268thread.popFrames(frame);269display(mthd.name() + " is resetted");270frame = thread.frame(0);271mthd = frame.location().method();272} while (mthd.isObsolete());273} catch (IncompatibleThreadStateException e) {274throw new Failure(UNEXPECTED_STRING + e);275}276display("");277}278279private StepRequest createStepRequest(ThreadReference thread) {280EventRequestManager evm = debugee.getEventRequestManager();281StepRequest request = evm.createStepRequest(thread,282StepRequest.STEP_LINE,283StepRequest.STEP_INTO);284request.enable();285return request;286}287288private void hitBreakpoint(BreakpointEvent event) {289locationInfo(event);290if (event.location().lineNumber() != tc08x001a.checkLastLine) {291complain("BreakpointEvent steps to line " + event.location().lineNumber()292+ ", expected line number is "293+ tc08x001a.checkLastLine);294exitStatus = Consts.TEST_FAILED;295} else {296display("!!!BreakpointEvent steps to the expected line "297+ event.location().lineNumber() + "!!!");298}299display("");300}301302private void hitStep(StepEvent event) {303locationInfo(event);304String methodName = event.location().method().name();305StepRequest request = (StepRequest )event.request();306307switch (eventCount) {308case 2:309checkLocMethod(methodName, firstMethodName);310break;311case 3:312checkLocMethod(methodName, firstMethodName);313Field fld = debugeeClass.fieldByName(tc08x001a.fieldToCheckName);314Value val = debugeeClass.getValue(fld);315if (((IntegerValue )val).value() != tc08x001a.CHANGED_VALUE) {316complain("Unexpected: new code is not actual");317complain("Unexpected value of checked field: "318+ val);319exitStatus = Consts.TEST_FAILED;320} else {321display("!!!Expected: Inserted line has worked");322}323request.disable();324break;325default:326complain("Unexpected event" + event);327exitStatus = Consts.TEST_FAILED;328}329display("");330}331332private void checkLocMethod(String currentMethodName, String expectedMethodName) {333if (currentMethodName.compareTo(expectedMethodName) != 0) {334complain("Unexpected event location at \"" + currentMethodName + "\"");335exitStatus = Consts.TEST_FAILED;336} else {337display("!!!Expected event location at \"" + currentMethodName + "\"");338}339}340341private void locationInfo(LocatableEvent event) {342eventCount++;343display("event info: #" + eventCount);344display("\tthread\t- " + event.thread().name());345try {346display("\tsource\t- " + event.location().sourceName());347display("\tmethod\t- " + event.location().method().name());348display("\tline\t- " + event.location().lineNumber());349} catch (AbsentInformationException e) {350}351}352}353354355