Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequest/disable/disable003.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.EventRequest.disable;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* The test checks up <code>com.sun.jdi.EventRequest.disable()</code> after38* class file redefinition.39*40* The test performs as follow:41* Debuggee has two classes. Debugger creates a <code>BreakpointRequest</code>42* at the first class and waits a corresponding event. After getting43* <code>BreakpointEvent</code>, debugger redefines the second class, tries to44* disable the set breakpoint and checks status of the <code>BreakpointRequest</code>45* to be disabled.46*/4748public class disable003 {4950public final static String UNEXPECTED_STRING = "***Unexpected exception ";5152private final static String prefix = "nsk.jdi.EventRequest.disable.";53private final static String debuggerName = prefix + "disable003";54public final static String debugeeName = debuggerName + "a";55public final static String testedClassName = debuggerName + "b";5657private final static String classFileName = "disable003b.class";58private final static String newClassFile = "newclass" + File.separator59+ prefix.replace('.',File.separatorChar)60+ classFileName;6162private static int exitStatus;63private static Log log;64private static Debugee debugee;65private static long waitTime;66private static String classDir;6768public final static int expectedEventCount = 1;69private ReferenceType debugeeClass;7071private static void display(String msg) {72log.display(msg);73}7475private static void complain(String msg) {76log.complain(msg + "\n");77}7879public static void main(String argv[]) {80System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));81}8283public static int run(String argv[], PrintStream out) {8485exitStatus = Consts.TEST_PASSED;8687disable003 thisTest = new disable003();8889ArgumentHandler argHandler = new ArgumentHandler(argv);90log = new Log(out, argHandler);9192classDir = argv[0];93waitTime = argHandler.getWaitTime() * 60000;9495Binder binder = new Binder(argHandler, log);96debugee = binder.bindToDebugee(debugeeName);97debugee.redirectOutput(log);9899try {100thisTest.execTest();101} catch (Throwable e) {102exitStatus = Consts.TEST_FAILED;103e.printStackTrace();104} finally {105debugee.endDebugee();106}107display("Test finished. exitStatus = " + exitStatus);108109return exitStatus;110}111112private void execTest() throws Failure {113114if (!debugee.VM().canRedefineClasses()) {115display("\n>>>canRedefineClasses() is false<<< test canceled.\n");116return;117}118119display("\nTEST BEGINS");120display("===========");121122EventSet eventSet = null;123EventIterator eventIterator = null;124Event event;125long totalTime = waitTime;126long tmp, begin = System.currentTimeMillis(),127delta = 0;128boolean exit = false;129130EventRequestManager evm = debugee.getEventRequestManager();131ClassPrepareRequest req = evm.createClassPrepareRequest();132req.addClassFilter(debugeeName);133req.enable();134debugee.resume();135136int eventCount = 0;137while (totalTime > 0 && !exit) {138if (eventIterator == null || !eventIterator.hasNext()) {139try {140eventSet = debugee.VM().eventQueue().remove(totalTime);141} catch (InterruptedException e) {142new Failure(e);143}144if (eventSet != null) {145eventIterator = eventSet.eventIterator();146} else {147eventIterator = null;148}149}150if (eventIterator != null) {151while (eventIterator.hasNext()) {152event = eventIterator.nextEvent();153// display("\nevent ===>>> " + event);154155if (event instanceof ClassPrepareEvent) {156display("\nevent ===>>> " + event);157prepareTestCases();158debugee.resume();159160} else if (event instanceof BreakpointEvent) {161display("\nevent ===>>> " + event);162ClassType testedClass;163testedClass = (ClassType )debugee.classByName(testedClassName);164165display("\nredefinition of the \""166+ testedClassName + "\" class");167redefineDebugee(testedClass, newClassFile);168169eventCount++;170boolean isBrkpEnabled = event.request().isEnabled();171if (!isBrkpEnabled) {172complain("breakpoint was disabled after "173+ "the redefinition of an other class");174exitStatus = Consts.TEST_FAILED;175} else {176display("\nis breakpoint enabled?\t" + isBrkpEnabled);177display("\ndisabling the breakpoint request...");178event.request().disable();179if (event.request().isEnabled()) {180complain("breakpoint is not disabled");181exitStatus = Consts.TEST_FAILED;182}183}184debugee.resume();185186} else if (event instanceof VMDeathEvent) {187exit = true;188break;189} else if (event instanceof VMDisconnectEvent) {190exit = true;191break;192} // if193} // while194} // if195tmp = System.currentTimeMillis();196delta = tmp - begin;197totalTime -= delta;198begin = tmp;199}200201if (totalTime <= 0) {202complain("out of wait time...");203exitStatus = Consts.TEST_FAILED;204}205if (eventCount != expectedEventCount) {206complain("expecting " + expectedEventCount207+ " events, but "208+ eventCount + " events arrived.");209exitStatus = Consts.TEST_FAILED;210}211212display("=============");213display("TEST FINISHES\n");214}215216private void redefineDebugee(ReferenceType refType, String classFileName) {217Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;218boolean alreadyComplained = false;219mapBytes = mapClassToBytes(refType, classFileName);220try {221debugee.VM().redefineClasses(mapBytes);222} catch (Exception e) {223throw new Failure(UNEXPECTED_STRING + e);224}225}226227private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(ReferenceType refType, String fileName) {228display("a new class-file\t:" + fileName);229File fileToBeRedefined = new File(classDir + File.separator + fileName);230int fileToRedefineLength = (int )fileToBeRedefined.length();231byte[] arrayToRedefine = new byte[fileToRedefineLength];232233FileInputStream inputFile;234try {235inputFile = new FileInputStream(fileToBeRedefined);236} catch (FileNotFoundException e) {237throw new Failure(UNEXPECTED_STRING + e);238}239240try {241inputFile.read(arrayToRedefine);242inputFile.close();243} catch (IOException e) {244throw new Failure(UNEXPECTED_STRING + e);245}246HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();247mapForClass.put(refType, arrayToRedefine);248return mapForClass;249}250251private void prepareTestCases() {252debugeeClass = debugee.classByName(debugeeName);253display("debugeeClass\t\t:" + debugeeClass.name());254display("setting breakpoint...");255debugee.setBreakpoint(debugeeClass,256disable003a.brkpMethodName,257disable003a.brkpLineNumber);258}259}260261262