jdk/test/java/lang/Thread/GenerifyStackTraces.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 23010 6dadb192ad81
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17482
diff changeset
     2
 * Copyright (c) 2004, 2013, 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: 4347
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
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
17482
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    26
 * @bug     4919105 8004177
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Generified basic unit test of Thread.getAllStackTraces()
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 GenerifyStackTraces {
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 String[] methodNames = {"run", "A", "B", "C", "Done"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static int DONE_DEPTH = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static Thread one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static boolean trace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        if (args.length > 0 && args[0].equals("trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            trace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        one = new ThreadOne();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        one.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
17482
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    50
        DumpThread dt = new DumpThread();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        dt.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
17482
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    53
        try {
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    54
            one.join();
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    55
        } finally {
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    56
            dt.shutdown();
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    57
        }
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    58
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new RuntimeException("Test Failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static class DumpThread extends Thread {
17482
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    65
        private volatile boolean finished = false;
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    66
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            int depth = 2;
17482
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    69
            while (!finished) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                // At each iterator, wait until ThreadOne blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                // to wait for thread dump.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                // Then dump stack trace and notify ThreadOne to continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    sleep(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    dumpStacks(depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    depth++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    finishDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
17482
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    84
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    85
        public void shutdown() throws InterruptedException {
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    86
            finished = true;
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    87
            this.join();
c3efc9e2e227 8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents: 7668
diff changeset
    88
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static class ThreadOne extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            A();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        private void A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            waitForDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            B();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        private void B() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            waitForDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            C();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        private void C() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            waitForDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            Done();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        private void Done() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            waitForDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // Get stack trace of current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            StackTraceElement[] stack = getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                checkStack(this, stack, DONE_DEPTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
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
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
   123
    private static void waitForDump() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        synchronized(go) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
               go.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
               throw new RuntimeException("Unexpected exception" + e);
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
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
   133
    private static void finishDump() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        synchronized(go) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
               go.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
               throw new RuntimeException("Unexpected exception" + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static void dumpStacks(int depth) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Get stack trace of another thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        StackTraceElement[] stack = one.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        checkStack(one, stack, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // Get stack traces of all Threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        for (Map.Entry<Thread, StackTraceElement[]> entry :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                 Thread.getAllStackTraces().entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            Thread t = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            stack = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (t == null || stack == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw new RuntimeException("Null thread or stacktrace returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (t == one) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                checkStack(t, stack, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static void checkStack(Thread t, StackTraceElement[] stack,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                   int depth) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            printStack(t, stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int frame = stack.length - 1;
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   169
        for (int i = 0; i < depth && frame >= 0; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (! stack[frame].getMethodName().equals(methodNames[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                throw new RuntimeException("Expected " + methodNames[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                           " in frame " + frame + " but got " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                           stack[frame].getMethodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            frame--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private static void printStack(Thread t, StackTraceElement[] stack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        System.out.println(t +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                           " stack: (length = " + stack.length + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            for (int j = 0; j < stack.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                System.out.println(stack[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}