jdk/test/java/lang/Thread/StackTraces.java
author chegar
Thu, 03 Mar 2011 16:48:53 +0000
changeset 8563 4b287f6663b6
parent 7668 d4a77089c587
permissions -rw-r--r--
6721694: java/lang/Thread/StartOOMTest.java fails with timeout or with crash Summary: the test is not suitable to be run automatically Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5808
diff changeset
     2
 * Copyright (c) 2003, 2010, 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     4593133
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic unit test of Thread.getStackTraces()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class StackTraces {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static Object go = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static Object dumpObj = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static String[] methodNames = {"run", "A", "B", "C", "Done"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static int DONE_DEPTH = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static Thread one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static boolean trace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        if (args.length > 0 && args[0].equals("trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            trace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        one = new ThreadOne();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        one.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Thread dt = new DumpThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        dt.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        dt.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            throw new RuntimeException("Test Failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static class DumpThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            int depth = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                // At each iterator, wait until ThreadOne blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                // to wait for thread dump.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                // Then dump stack trace and notify ThreadOne to continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    sleep(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    dumpStacks(depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    depth++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    finishDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static class ThreadOne extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            A();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        private void A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            waitForDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            B();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        private void B() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            waitForDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            C();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        private void C() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            waitForDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            Done();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        private void Done() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            waitForDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // Get stack trace of current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            StackTraceElement[] stack = getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                checkStack(this, stack, DONE_DEPTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static private void waitForDump() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        synchronized(go) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
               go.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
               throw new RuntimeException("Unexpected exception" + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static private void finishDump() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        synchronized(go) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
               go.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
               throw new RuntimeException("Unexpected exception" + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static void dumpStacks(int depth) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // Get stack trace of another thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        StackTraceElement[] stack = one.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        checkStack(one, stack, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // Get stack traces of all Threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Map m = Thread.getAllStackTraces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Set s = m.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Iterator iter = s.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Map.Entry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            entry = (Map.Entry) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Thread t = (Thread) entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            stack = (StackTraceElement[]) entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (t == null || stack == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
               throw new RuntimeException("Null thread or stacktrace returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (t == one) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                checkStack(t, stack, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static void checkStack(Thread t, StackTraceElement[] stack,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                   int depth) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            printStack(t, stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int frame = stack.length - 1;
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   162
        for (int i = 0; i < depth && frame >= 0; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (! stack[frame].getMethodName().equals(methodNames[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                throw new RuntimeException("Expected " + methodNames[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                           " in frame " + frame + " but got " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                           stack[frame].getMethodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            frame--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static void printStack(Thread t, StackTraceElement[] stack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        System.out.println(t +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                           " stack: (length = " + stack.length + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            for (int j = 0; j < stack.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                System.out.println(stack[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}