jdk/test/com/sun/jdi/FinalizerTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 24973 8c4bc3fa4c4e
child 44423 306c020eb154
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 24973
diff changeset
     2
 * Copyright (c) 1999, 2015, 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
 *
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 24973
diff changeset
    30
 *  @modules jdk.jdi
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run compile -g FinalizerTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
24973
8c4bc3fa4c4e 6622468: TEST_BUG: Time to retire the @debuggeeVMOptions mechanism used in the com.sun.jdi infrastructure
sla
parents: 5506
diff changeset
    34
 *  @run driver FinalizerTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Debuggee which exercises a finalize() method. There's no guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * that this will work, but we need some way of attempting to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the debugging of finalizers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Gordon Hirsch  (modified for HotSpot by tbell & rfield)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class FinalizerTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static String lockit = "lock";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static boolean finalizerRun = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static class BigObject {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        byte[] foo = new byte[300000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public BigObject (String _name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            this.name = _name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
             * JLS 2nd Ed. section 12.6 "Finalization of Class Instances" "[...]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
             * invoke the finalize method for its superclass, [...] usually good
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
             * practice [...]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            //Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            finalizerRun = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static void waitForAFinalizer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        String s = Integer.toString(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        BigObject b = new BigObject (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        b = null; // Drop the object, creating garbage...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.runFinalization();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // Now, we have to make sure the finalizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // gets run.  We will keep allocating more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // and more memory with the idea that eventually,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // the memory occupied by the BigObject will get reclaimed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // and the finalizer will be run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        List holdAlot = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        for (int chunk=10000000; chunk > 10000; chunk = chunk / 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (finalizerRun) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    holdAlot.add(new byte[chunk]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    System.err.println("Allocated " + chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            catch ( Throwable thrown ) {  // OutOfMemoryError
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            System.runFinalization();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return;  // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * Spin in waitForAFinalizer() while waiting for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * another thread to run the finalizer on one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         * BigObjects ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        waitForAFinalizer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
///// End of debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
public class FinalizerTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static void main(String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        new FinalizerTest (args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public FinalizerTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            BreakpointEvent event0 = startToMain("FinalizerTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            BreakpointEvent event1 = resumeTo("FinalizerTarg$BigObject",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                              "finalize", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            println("Breakpoint at " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    event1.location().method().name() + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    event1.location().lineNumber() + " (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    event1.location().codeIndex() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             * Record information about the current location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            List frames = event1.thread().frames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            List methodStack = new ArrayList(frames.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Iterator iter = frames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                StackFrame frame = (StackFrame) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                methodStack.add(frame.location().declaringType().name() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                "." + frame.location().method().name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            println("Try a stepOverLine()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            StepEvent stepEvent = stepOverLine(event1.thread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            println("Step Complete at " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                               stepEvent.location().method().name() + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                               stepEvent.location().lineNumber() + " (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                               stepEvent.location().codeIndex() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
             * Compare current location with recorded location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (stepEvent.thread().frameCount() != methodStack.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                throw new Exception("Stack depths do not match: original=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                    methodStack.size() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                    ", current=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                    stepEvent.thread().frameCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            iter = stepEvent.thread().frames().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            Iterator iter2 = methodStack.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                StackFrame frame = (StackFrame) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                String name = (String) iter2.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                String currentName = frame.location().declaringType().name() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                "." + frame.location().method().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                if (!name.equals(currentName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    throw new Exception("Stacks do not match at: original=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                         name + ", current=" + currentName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch(Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            // Allow application to complete and shut down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            println("FinalizerTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new Exception("FinalizerTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}