Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jdi/FinalizerTest.java
38855 views
/*1* Copyright (c) 1999, 2001, 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*/2223/**24* @test25* @bug 4272800 4274208 439201026* @summary Test debugger operations in finalize() methods27* @author Gordon Hirsch (modified for HotSpot by tbell & rfield)28*29* @run build TestScaffold VMConnection TargetListener TargetAdapter30* @run compile -g FinalizerTest.java31*32* @run main FinalizerTest33*/34import com.sun.jdi.*;35import com.sun.jdi.event.*;36import com.sun.jdi.request.*;3738import java.util.List;39import java.util.ArrayList;40import java.util.Iterator;414243/*44* Debuggee which exercises a finalize() method. There's no guarantee45* that this will work, but we need some way of attempting to test46* the debugging of finalizers.47* @author Gordon Hirsch (modified for HotSpot by tbell & rfield)48*/49class FinalizerTarg {50static String lockit = "lock";51static boolean finalizerRun = false;52static class BigObject {53String name;54byte[] foo = new byte[300000];5556public BigObject (String _name) {57super();58this.name = _name;59}6061protected void finalize() throws Throwable {62/*63* JLS 2nd Ed. section 12.6 "Finalization of Class Instances" "[...]64* invoke the finalize method for its superclass, [...] usually good65* practice [...]"66*/67super.finalize();68//Thread.dumpStack();69finalizerRun = true;70}71}7273static void waitForAFinalizer() {74String s = Integer.toString(1);75BigObject b = new BigObject (s);76b = null; // Drop the object, creating garbage...77System.gc();78System.runFinalization();7980// Now, we have to make sure the finalizer81// gets run. We will keep allocating more82// and more memory with the idea that eventually,83// the memory occupied by the BigObject will get reclaimed84// and the finalizer will be run.85List holdAlot = new ArrayList();86for (int chunk=10000000; chunk > 10000; chunk = chunk / 2) {87if (finalizerRun) {88return;89}90try {91while(true) {92holdAlot.add(new byte[chunk]);93System.err.println("Allocated " + chunk);94}95}96catch ( Throwable thrown ) { // OutOfMemoryError97System.gc();98}99System.runFinalization();100}101return; // not reached102}103104public static void main(String[] args) throws Exception {105/*106* Spin in waitForAFinalizer() while waiting for107* another thread to run the finalizer on one of the108* BigObjects ...109*/110waitForAFinalizer();111}112}113///// End of debuggee114115116public class FinalizerTest extends TestScaffold {117118public static void main(String args[])119throws Exception {120new FinalizerTest (args).startTests();121}122123public FinalizerTest (String args[]) {124super(args);125}126127protected void runTests() throws Exception {128try {129BreakpointEvent event0 = startToMain("FinalizerTarg");130131BreakpointEvent event1 = resumeTo("FinalizerTarg$BigObject",132"finalize", "()V");133134println("Breakpoint at " +135event1.location().method().name() + ":" +136event1.location().lineNumber() + " (" +137event1.location().codeIndex() + ")");138139/*140* Record information about the current location141*/142List frames = event1.thread().frames();143List methodStack = new ArrayList(frames.size());144Iterator iter = frames.iterator();145while (iter.hasNext()) {146StackFrame frame = (StackFrame) iter.next();147methodStack.add(frame.location().declaringType().name() +148"." + frame.location().method().name());149}150println("Try a stepOverLine()...");151StepEvent stepEvent = stepOverLine(event1.thread());152153println("Step Complete at " +154stepEvent.location().method().name() + ":" +155stepEvent.location().lineNumber() + " (" +156stepEvent.location().codeIndex() + ")");157158/*159* Compare current location with recorded location160*/161if (stepEvent.thread().frameCount() != methodStack.size()) {162throw new Exception("Stack depths do not match: original=" +163methodStack.size() +164", current=" +165stepEvent.thread().frameCount());166}167iter = stepEvent.thread().frames().iterator();168Iterator iter2 = methodStack.iterator();169while (iter.hasNext()) {170StackFrame frame = (StackFrame) iter.next();171String name = (String) iter2.next();172String currentName = frame.location().declaringType().name() +173"." + frame.location().method().name();174if (!name.equals(currentName)) {175throw new Exception("Stacks do not match at: original=" +176name + ", current=" + currentName);177178}179}180} catch(Exception ex) {181ex.printStackTrace();182testFailed = true;183} finally {184// Allow application to complete and shut down185listenUntilVMDisconnect();186}187if (!testFailed) {188println("FinalizerTest: passed");189} else {190throw new Exception("FinalizerTest: failed");191}192}193}194195196