1 /*
   2  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     4919105 8004177
  27  * @summary Generified basic unit test of Thread.getAllStackTraces()
  28  * @author  Mandy Chung
  29  */
  30 
  31 import java.util.*;
  32 
  33 public class GenerifyStackTraces {
  34 
  35     private static Object go = new Object();
  36     private static volatile boolean finished = false;
  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         one.join();
  56         dt.join();
  57         if (testFailed) {
  58             throw new RuntimeException("Test Failed.");
  59         }
  60     }
  61 
  62     static class DumpThread extends Thread {
  63         public void run() {
  64             int depth = 2;
  65             while (!finished) {
  66                 // At each iterator, wait until ThreadOne blocks
  67                 // to wait for thread dump.
  68                 // Then dump stack trace and notify ThreadOne to continue.
  69                 try {
  70                     sleep(2000);
  71                     dumpStacks(depth);
  72                     depth++;
  73                     finishDump();
  74                 } catch (Exception e) {
  75                     e.printStackTrace();
  76                     testFailed = true;
  77                 }
  78             }
  79         }
  80     }
  81 
  82     static class ThreadOne extends Thread {
  83         public void run() {
  84             try {
  85                 A();
  86             } finally {
  87                 finished = true;
  88             }
  89         }
  90         private void A() {
  91             waitForDump();
  92             B();
  93         }
  94         private void B() {
  95             waitForDump();
  96             C();
  97         }
  98         private void C() {
  99             waitForDump();
 100             Done();
 101         }
 102         private void Done() {
 103             waitForDump();
 104 
 105             // Get stack trace of current thread
 106             StackTraceElement[] stack = getStackTrace();
 107             try {
 108                 checkStack(this, stack, DONE_DEPTH);
 109             } catch (Exception e) {
 110                 e.printStackTrace();
 111                 testFailed = true;
 112             }
 113         }
 114 
 115     }
 116 
 117 
 118     static private void waitForDump() {
 119         synchronized(go) {
 120             try {
 121                go.wait();
 122             } catch (Exception e) {
 123                throw new RuntimeException("Unexpected exception" + e);
 124             }
 125         }
 126     }
 127 
 128     static private void finishDump() {
 129         synchronized(go) {
 130             try {
 131                go.notifyAll();
 132             } catch (Exception e) {
 133                throw new RuntimeException("Unexpected exception" + e);
 134             }
 135         }
 136     }
 137 
 138     public static void dumpStacks(int depth) throws Exception {
 139         // Get stack trace of another thread
 140         StackTraceElement[] stack = one.getStackTrace();
 141         checkStack(one, stack, depth);
 142 
 143 
 144         // Get stack traces of all Threads
 145         for (Map.Entry<Thread, StackTraceElement[]> entry :
 146                  Thread.getAllStackTraces().entrySet()) {
 147             Thread t = entry.getKey();
 148             stack = entry.getValue();
 149             if (t == null || stack == null) {
 150                 throw new RuntimeException("Null thread or stacktrace returned");
 151             }
 152             if (t == one) {
 153                 checkStack(t, stack, depth);
 154             }
 155         }
 156     }
 157 
 158     private static void checkStack(Thread t, StackTraceElement[] stack,
 159                                    int depth) throws Exception {
 160         if (trace) {
 161             printStack(t, stack);
 162         }
 163         int frame = stack.length - 1;
 164         for (int i = 0; i < depth && frame >= 0; i++) {
 165             if (! stack[frame].getMethodName().equals(methodNames[i])) {
 166                 throw new RuntimeException("Expected " + methodNames[i] +
 167                                            " in frame " + frame + " but got " +
 168                                            stack[frame].getMethodName());
 169             }
 170             frame--;
 171         }
 172     }
 173 
 174     private static void printStack(Thread t, StackTraceElement[] stack) {
 175         System.out.println(t +
 176                            " stack: (length = " + stack.length + ")");
 177         if (t != null) {
 178             for (int j = 0; j < stack.length; j++) {
 179                 System.out.println(stack[j]);
 180             }
 181             System.out.println();
 182         }
 183     }
 184 }