Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/deleteEventRequest/delevtreq003.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*/22package nsk.jdi.EventRequestManager.deleteEventRequest;2324import nsk.share.*;25import nsk.share.jpda.*;26import nsk.share.jdi.*;2728import com.sun.jdi.*;29import com.sun.jdi.request.*;30import com.sun.jdi.event.*;3132import java.util.*;33import java.io.*;3435/**36* The test checks up <code>com.sun.jdi.EventRequestManager.deleteEventRequest()</code>37* after class file redefinition.38*39* The test performs as follow:40* Debuggee has two classes. Debugger creates a <code>BreakpointRequest</code> at the41* first class and waits a corresponding event. After getting <code>BreakpointEvent</code>,42* debugger redefines the second class and tries to delete <code>BreakpointRequest</code>.43*/4445public class delevtreq003 {4647public final static String UNEXPECTED_STRING = "***Unexpected exception ";4849private final static String prefix = "nsk.jdi.EventRequestManager.deleteEventRequest.";50private final static String debuggerName = prefix + "delevtreq003";51public final static String debugeeName = debuggerName + "a";52public final static String testedClassName = debuggerName + "b";5354private final static String classFileName = "delevtreq003b.class";55private final static String newClassFile = "newclass" + File.separator56+ prefix.replace('.',File.separatorChar)57+ classFileName;5859private static int exitStatus;60private static Log log;61private static Debugee debugee;62private static long waitTime;63private static String classDir;6465public final static int expectedEventCount = 1;66private ReferenceType debugeeClass;6768private static void display(String msg) {69log.display(msg);70}7172private static void complain(String msg) {73log.complain(msg + "\n");74}7576public static void main(String argv[]) {77System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));78}7980public static int run(String argv[], PrintStream out) {8182exitStatus = Consts.TEST_PASSED;8384delevtreq003 thisTest = new delevtreq003();8586ArgumentHandler argHandler = new ArgumentHandler(argv);87log = new Log(out, argHandler);8889classDir = argv[0];90waitTime = argHandler.getWaitTime() * 60000;9192Binder binder = new Binder(argHandler, log);93debugee = binder.bindToDebugee(debugeeName);94debugee.redirectOutput(log);9596try {97thisTest.execTest();98} catch (Throwable e) {99exitStatus = Consts.TEST_FAILED;100e.printStackTrace();101} finally {102debugee.endDebugee();103}104display("Test finished. exitStatus = " + exitStatus);105106return exitStatus;107}108109private void execTest() throws Failure {110111if (!debugee.VM().canRedefineClasses()) {112display("\n>>>canRedefineClasses() is false<<< test canceled.\n");113return;114}115116display("\nTEST BEGINS");117display("===========");118119EventSet eventSet = null;120EventIterator eventIterator = null;121Event event;122long totalTime = waitTime;123long tmp, begin = System.currentTimeMillis(),124delta = 0;125boolean exit = false;126127EventRequestManager evm = debugee.getEventRequestManager();128ClassPrepareRequest req = evm.createClassPrepareRequest();129req.addClassFilter(debugeeName);130req.enable();131debugee.resume();132133int eventCount = 0;134while (totalTime > 0 && !exit) {135if (eventIterator == null || !eventIterator.hasNext()) {136try {137eventSet = debugee.VM().eventQueue().remove(totalTime);138} catch (InterruptedException e) {139new Failure(e);140}141if (eventSet != null) {142eventIterator = eventSet.eventIterator();143} else {144eventIterator = null;145}146}147if (eventIterator != null) {148while (eventIterator.hasNext()) {149event = eventIterator.nextEvent();150// display("\nevent ===>>> " + event);151152if (event instanceof ClassPrepareEvent) {153display("\nevent ===>>> " + event);154prepareTestCases();155debugee.resume();156157} else if (event instanceof BreakpointEvent) {158display("\nevent ===>>> " + event);159ClassType testedClass;160testedClass = (ClassType )debugee.classByName(testedClassName);161162display("\nredefining...");163redefineDebugee(testedClass, newClassFile);164165eventCount++;166boolean isBrkpEnabled = event.request().isEnabled();167if (!isBrkpEnabled) {168complain("breakpoint was disabled after "169+ "the redefinition of an other class");170exitStatus = Consts.TEST_FAILED;171} else {172display("\nis breakpoint enabled?\t" + isBrkpEnabled);173display("\ndeleting the breakpoint request...");174evm.deleteEventRequest(event.request());175}176debugee.resume();177178} else if (event instanceof VMDeathEvent) {179exit = true;180break;181} else if (event instanceof VMDisconnectEvent) {182exit = true;183break;184} // if185} // while186} // if187tmp = System.currentTimeMillis();188delta = tmp - begin;189totalTime -= delta;190begin = tmp;191}192193if (totalTime <= 0) {194complain("out of wait time...");195exitStatus = Consts.TEST_FAILED;196}197if (eventCount != expectedEventCount) {198complain("expecting " + expectedEventCount199+ " events, but "200+ eventCount + " events arrived.");201exitStatus = Consts.TEST_FAILED;202}203204display("=============");205display("TEST FINISHES\n");206}207208private void redefineDebugee(ReferenceType refType, String classFileName) {209Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;210boolean alreadyComplained = false;211mapBytes = mapClassToBytes(refType, classFileName);212try {213debugee.VM().redefineClasses(mapBytes);214} catch (Exception e) {215throw new Failure(UNEXPECTED_STRING + e);216}217}218219private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(ReferenceType refType, String fileName) {220display("class-file\t:" + fileName);221File fileToBeRedefined = new File(classDir + File.separator + fileName);222int fileToRedefineLength = (int )fileToBeRedefined.length();223byte[] arrayToRedefine = new byte[fileToRedefineLength];224225FileInputStream inputFile;226try {227inputFile = new FileInputStream(fileToBeRedefined);228} catch (FileNotFoundException e) {229throw new Failure(UNEXPECTED_STRING + e);230}231232try {233inputFile.read(arrayToRedefine);234inputFile.close();235} catch (IOException e) {236throw new Failure(UNEXPECTED_STRING + e);237}238HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();239mapForClass.put(refType, arrayToRedefine);240return mapForClass;241}242243private void prepareTestCases() {244debugeeClass = debugee.classByName(debugeeName);245display("debugeeClass\t\t:" + debugeeClass.name());246display("setting breakpoint...");247debugee.setBreakpoint(debugeeClass,248delevtreq003a.brkpMethodName,249delevtreq003a.brkpLineNumber);250}251}252253254