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
  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 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 
 138         // Get stack traces of all Threads
 139         for (Map.Entry<Thread, StackTraceElement[]> entry :
 140                  Thread.getAllStackTraces().entrySet()) {
 141             Thread t = entry.getKey();
 142             stack = entry.getValue();
 143             if (t == null || stack == null) {
 144                 throw new RuntimeException("Null thread or stacktrace returned");
 145             }
 146             if (t == one) {
 147                 checkStack(t, stack, depth);
 148             }
 149         }
 150     }
 151 
 152     private static void checkStack(Thread t, StackTraceElement[] stack,
 153                                    int depth) throws Exception {
 154         if (trace) {
 155             printStack(t, stack);
 156         }
 157         int frame = stack.length - 1;
 158         for (int i = 0; i < depth && frame >= 0; i++) {
 159             if (! stack[frame].getMethodName().equals(methodNames[i])) {
 160                 throw new RuntimeException("Expected " + methodNames[i] +
 161                                            " in frame " + frame + " but got " +
 162                                            stack[frame].getMethodName());
 163             }
 164             frame--;
 165         }
 166     }
 167 
 168     private static void printStack(Thread t, StackTraceElement[] stack) {
 169         System.out.println(t +
 170                            " stack: (length = " + stack.length + ")");
 171         if (t != null) {
 172             for (int j = 0; j < stack.length; j++) {
 173                 System.out.println(stack[j]);
 174             }
 175             System.out.println();
 176         }
 177     }
 178 }