0N/A/*
3261N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4593133
0N/A * @summary Basic unit test of Thread.getStackTraces()
0N/A * @author Mandy Chung
0N/A */
0N/A
0N/Aimport java.util.*;
0N/A
0N/Apublic class StackTraces {
0N/A
0N/A private static Object go = new Object();
0N/A private static Object dumpObj = new Object();
0N/A private static String[] methodNames = {"run", "A", "B", "C", "Done"};
0N/A private static int DONE_DEPTH = 5;
0N/A private static boolean testFailed = false;
0N/A
0N/A private static Thread one;
0N/A private static boolean trace = false;
0N/A public static void main(String[] args) throws Exception {
0N/A if (args.length > 0 && args[0].equals("trace")) {
0N/A trace = true;
0N/A }
0N/A
0N/A one = new ThreadOne();
0N/A one.start();
0N/A
0N/A Thread dt = new DumpThread();
0N/A dt.setDaemon(true);
0N/A dt.start();
0N/A
0N/A if (testFailed) {
0N/A throw new RuntimeException("Test Failed.");
0N/A }
0N/A }
0N/A
0N/A static class DumpThread extends Thread {
0N/A public void run() {
0N/A int depth = 2;
0N/A while (true) {
0N/A // At each iterator, wait until ThreadOne blocks
0N/A // to wait for thread dump.
0N/A // Then dump stack trace and notify ThreadOne to continue.
0N/A try {
0N/A sleep(2000);
0N/A dumpStacks(depth);
0N/A depth++;
0N/A finishDump();
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A testFailed = true;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A static class ThreadOne extends Thread {
0N/A public void run() {
0N/A A();
0N/A }
0N/A private void A() {
0N/A waitForDump();
0N/A B();
0N/A }
0N/A private void B() {
0N/A waitForDump();
0N/A C();
0N/A }
0N/A private void C() {
0N/A waitForDump();
0N/A Done();
0N/A }
0N/A private void Done() {
0N/A waitForDump();
0N/A
0N/A // Get stack trace of current thread
0N/A StackTraceElement[] stack = getStackTrace();
0N/A try {
0N/A checkStack(this, stack, DONE_DEPTH);
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A testFailed = true;
0N/A }
0N/A }
0N/A
0N/A }
0N/A
0N/A
0N/A static private void waitForDump() {
0N/A synchronized(go) {
0N/A try {
0N/A go.wait();
0N/A } catch (Exception e) {
0N/A throw new RuntimeException("Unexpected exception" + e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A static private void finishDump() {
0N/A synchronized(go) {
0N/A try {
0N/A go.notifyAll();
0N/A } catch (Exception e) {
0N/A throw new RuntimeException("Unexpected exception" + e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static void dumpStacks(int depth) throws Exception {
0N/A // Get stack trace of another thread
0N/A StackTraceElement[] stack = one.getStackTrace();
0N/A checkStack(one, stack, depth);
0N/A
0N/A // Get stack traces of all Threads
0N/A Map m = Thread.getAllStackTraces();
0N/A Set s = m.entrySet();
0N/A Iterator iter = s.iterator();
0N/A
0N/A Map.Entry entry;
0N/A while (iter.hasNext()) {
0N/A entry = (Map.Entry) iter.next();
0N/A Thread t = (Thread) entry.getKey();
0N/A stack = (StackTraceElement[]) entry.getValue();
0N/A if (t == null || stack == null) {
0N/A throw new RuntimeException("Null thread or stacktrace returned");
0N/A }
0N/A if (t == one) {
0N/A checkStack(t, stack, depth);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void checkStack(Thread t, StackTraceElement[] stack,
0N/A int depth) throws Exception {
0N/A if (trace) {
0N/A printStack(t, stack);
0N/A }
0N/A int frame = stack.length - 1;
2495N/A for (int i = 0; i < depth && frame >= 0; i++) {
0N/A if (! stack[frame].getMethodName().equals(methodNames[i])) {
0N/A throw new RuntimeException("Expected " + methodNames[i] +
0N/A " in frame " + frame + " but got " +
0N/A stack[frame].getMethodName());
0N/A }
0N/A frame--;
0N/A }
0N/A }
0N/A
0N/A private static void printStack(Thread t, StackTraceElement[] stack) {
0N/A System.out.println(t +
0N/A " stack: (length = " + stack.length + ")");
0N/A if (t != null) {
0N/A for (int j = 0; j < stack.length; j++) {
0N/A System.out.println(stack[j]);
0N/A }
0N/A System.out.println();
0N/A }
0N/A }
0N/A}