jdk/test/com/sun/jdi/FinalizerTest.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 24973 8c4bc3fa4c4e
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *  @bug 4272800 4274208 4392010
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary Test debugger operations in finalize() methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  @author Gordon Hirsch  (modified for HotSpot by tbell & rfield)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run compile -g FinalizerTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main FinalizerTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Debuggee which exercises a finalize() method. There's no guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * that this will work, but we need some way of attempting to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * the debugging of finalizers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Gordon Hirsch  (modified for HotSpot by tbell & rfield)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
class FinalizerTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static String lockit = "lock";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static boolean finalizerRun = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static class BigObject {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        byte[] foo = new byte[300000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        public BigObject (String _name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            this.name = _name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
             * JLS 2nd Ed. section 12.6 "Finalization of Class Instances" "[...]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
             * invoke the finalize method for its superclass, [...] usually good
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
             * practice [...]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            //Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            finalizerRun = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static void waitForAFinalizer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String s = Integer.toString(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        BigObject b = new BigObject (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        b = null; // Drop the object, creating garbage...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.runFinalization();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // Now, we have to make sure the finalizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // gets run.  We will keep allocating more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // and more memory with the idea that eventually,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // the memory occupied by the BigObject will get reclaimed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // and the finalizer will be run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        List holdAlot = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (int chunk=10000000; chunk > 10000; chunk = chunk / 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (finalizerRun) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    holdAlot.add(new byte[chunk]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    System.err.println("Allocated " + chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            catch ( Throwable thrown ) {  // OutOfMemoryError
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            System.runFinalization();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return;  // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * Spin in waitForAFinalizer() while waiting for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * another thread to run the finalizer on one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * BigObjects ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        waitForAFinalizer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
///// End of debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
public class FinalizerTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public static void main(String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        new FinalizerTest (args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public FinalizerTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            BreakpointEvent event0 = startToMain("FinalizerTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            BreakpointEvent event1 = resumeTo("FinalizerTarg$BigObject",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                              "finalize", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            println("Breakpoint at " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    event1.location().method().name() + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    event1.location().lineNumber() + " (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    event1.location().codeIndex() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             * Record information about the current location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            List frames = event1.thread().frames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            List methodStack = new ArrayList(frames.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Iterator iter = frames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                StackFrame frame = (StackFrame) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                methodStack.add(frame.location().declaringType().name() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                "." + frame.location().method().name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            println("Try a stepOverLine()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            StepEvent stepEvent = stepOverLine(event1.thread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            println("Step Complete at " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                               stepEvent.location().method().name() + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                               stepEvent.location().lineNumber() + " (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                               stepEvent.location().codeIndex() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
             * Compare current location with recorded location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (stepEvent.thread().frameCount() != methodStack.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw new Exception("Stack depths do not match: original=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                    methodStack.size() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                    ", current=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                    stepEvent.thread().frameCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            iter = stepEvent.thread().frames().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            Iterator iter2 = methodStack.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                StackFrame frame = (StackFrame) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                String name = (String) iter2.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                String currentName = frame.location().declaringType().name() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                "." + frame.location().method().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (!name.equals(currentName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    throw new Exception("Stacks do not match at: original=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                         name + ", current=" + currentName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } catch(Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // Allow application to complete and shut down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            println("FinalizerTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new Exception("FinalizerTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}