J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4593133 |
| 27 | * @summary Basic unit test of Thread.getStackTraces() |
| 28 | * @author Mandy Chung |
| 29 | */ |
| 30 | |
| 31 | import java.util.*; |
| 32 | |
| 33 | public class StackTraces { |
| 34 | |
| 35 | private static Object go = new Object(); |
| 36 | private static Object dumpObj = new Object(); |
| 37 | private static String[] methodNames = {"run", "A", "B", "C", "Done"}; |
| 38 | private static int DONE_DEPTH = 5; |
| 39 | private static boolean testFailed = false; |
| 40 | |
| 41 | private static Thread one; |
| 42 | private static boolean trace = false; |
| 43 | public static void main(String[] args) throws Exception { |
| 44 | if (args.length > 0 && args[0].equals("trace")) { |
| 45 | trace = true; |
| 46 | } |
| 47 | |
| 48 | one = new ThreadOne(); |
| 49 | one.start(); |
| 50 | |
| 51 | Thread dt = new DumpThread(); |
| 52 | dt.setDaemon(true); |
| 53 | dt.start(); |
| 54 | |
| 55 | if (testFailed) { |
| 56 | throw new RuntimeException("Test Failed."); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static class DumpThread extends Thread { |
| 61 | public void run() { |
| 62 | int depth = 2; |
| 63 | while (true) { |
| 64 | // At each iterator, wait until ThreadOne blocks |
| 65 | // to wait for thread dump. |
| 66 | // Then dump stack trace and notify ThreadOne to continue. |
| 67 | try { |
| 68 | sleep(2000); |
| 69 | dumpStacks(depth); |
| 70 | depth++; |
| 71 | finishDump(); |
| 72 | } catch (Exception e) { |
| 73 | e.printStackTrace(); |
| 74 | testFailed = true; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static class ThreadOne extends Thread { |
| 81 | public void run() { |
| 82 | A(); |
| 83 | } |
| 84 | private void A() { |
| 85 | waitForDump(); |
| 86 | B(); |
| 87 | } |
| 88 | private void B() { |
| 89 | waitForDump(); |
| 90 | C(); |
| 91 | } |
| 92 | private void C() { |
| 93 | waitForDump(); |
| 94 | Done(); |
| 95 | } |
| 96 | private void Done() { |
| 97 | waitForDump(); |
| 98 | |
| 99 | // Get stack trace of current thread |
| 100 | StackTraceElement[] stack = getStackTrace(); |
| 101 | try { |
| 102 | checkStack(this, stack, DONE_DEPTH); |
| 103 | } catch (Exception e) { |
| 104 | e.printStackTrace(); |
| 105 | testFailed = true; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | |
| 112 | static private void waitForDump() { |
| 113 | synchronized(go) { |
| 114 | try { |
| 115 | go.wait(); |
| 116 | } catch (Exception e) { |
| 117 | throw new RuntimeException("Unexpected exception" + e); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static private void finishDump() { |
| 123 | synchronized(go) { |
| 124 | try { |
| 125 | go.notifyAll(); |
| 126 | } catch (Exception e) { |
| 127 | throw new RuntimeException("Unexpected exception" + e); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public static void dumpStacks(int depth) throws Exception { |
| 133 | // Get stack trace of another thread |
| 134 | StackTraceElement[] stack = one.getStackTrace(); |
| 135 | checkStack(one, stack, depth); |
| 136 | |
| 137 | // Get stack traces of all Threads |
| 138 | Map m = Thread.getAllStackTraces(); |
| 139 | Set s = m.entrySet(); |
| 140 | Iterator iter = s.iterator(); |
| 141 | |
| 142 | Map.Entry entry; |
| 143 | while (iter.hasNext()) { |
| 144 | entry = (Map.Entry) iter.next(); |
| 145 | Thread t = (Thread) entry.getKey(); |
| 146 | stack = (StackTraceElement[]) entry.getValue(); |
| 147 | if (t == null || stack == null) { |
| 148 | throw new RuntimeException("Null thread or stacktrace returned"); |
| 149 | } |
| 150 | if (t == one) { |
| 151 | checkStack(t, stack, depth); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private static void checkStack(Thread t, StackTraceElement[] stack, |
| 157 | int depth) throws Exception { |
| 158 | if (trace) { |
| 159 | printStack(t, stack); |
| 160 | } |
| 161 | int frame = stack.length - 1; |
| 162 | for (int i = 0; i < depth; i++) { |
| 163 | if (! stack[frame].getMethodName().equals(methodNames[i])) { |
| 164 | throw new RuntimeException("Expected " + methodNames[i] + |
| 165 | " in frame " + frame + " but got " + |
| 166 | stack[frame].getMethodName()); |
| 167 | } |
| 168 | frame--; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | private static void printStack(Thread t, StackTraceElement[] stack) { |
| 173 | System.out.println(t + |
| 174 | " stack: (length = " + stack.length + ")"); |
| 175 | if (t != null) { |
| 176 | for (int j = 0; j < stack.length; j++) { |
| 177 | System.out.println(stack[j]); |
| 178 | } |
| 179 | System.out.println(); |
| 180 | } |
| 181 | } |
| 182 | } |