Example usage for java.lang Thread getStackTrace

List of usage examples for java.lang Thread getStackTrace

Introduction

In this page you can find the example usage for java.lang Thread getStackTrace.

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Returns an array of stack trace elements representing the stack dump of this thread.

Usage

From source file:Main.java

/**
 * Determines the name of the invoking method.<br />
 * <br />//from w  w w. j a  v  a2 s. c  om
 * <i><u>Note:</u><br />
 * The method looks for the method name within the current thread's stack trace. The
 * current implementation looks for the name at a fixed position.</i>
 *
 * @return a method name
 */
public static String getMethodName() {

    Thread currentThread = Thread.currentThread();
    StackTraceElement[] stackTrace = currentThread.getStackTrace();

    int elements = stackTrace.length;
    int expectedIndex = 3;

    if (expectedIndex < elements) {

        StackTraceElement element = stackTrace[expectedIndex];

        String[] substrings = element.toString().split("\\(");
        String name = substrings[0];

        return name;
    }

    return "unknown method";
}

From source file:com.clustercontrol.selfcheck.monitor.ThreadActivityMonitor.java

private static String getStackTrace(Thread t) {
    StackTraceElement[] eList = t.getStackTrace();
    StringBuilder trace = new StringBuilder();
    for (StackTraceElement e : eList) {
        trace.append("\n\tat ");
        trace.append(e.getClassName() + "." + e.getMethodName() + "(" + e.getFileName() + ":"
                + e.getLineNumber() + ")");
    }// www. j  a  v a  2  s  .  c o  m
    return trace.toString();
}

From source file:Main.java

public static String dumpStack(Thread t) {
    StringBuilder sb = new StringBuilder();
    sb.append(t.getName() + "[" + t.getId() + "] - " + t.getState() + ":\n");
    for (StackTraceElement ste : t.getStackTrace()) {
        sb.append("\tat " + ste.getClassName() + "." + ste.getMethodName() + "(" + ste.getFileName() + ":"
                + ste.getLineNumber() + ")\n");
    }/*from www  . ja  v  a2 s .co  m*/
    return sb.toString();
}

From source file:Main.java

/**
 * @param thread a thread/*from   w  w w  .j  av a  2s.c  o  m*/
 * @return a human-readable representation of the thread's stack trace
 */
public static String formatStackTrace(Thread thread) {
    Throwable t = new Throwable(
            String.format("Stack trace for thread %s (State: %s):", thread.getName(), thread.getState()));
    t.setStackTrace(thread.getStackTrace());
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:edu.wisc.commons.httpclient.CleanShutdownPoolingClientConnectionManager.java

private static String getStackTrace(Thread t) {
    final StringBuilder traceBuilder = new StringBuilder();

    final StackTraceElement[] trace = t.getStackTrace();
    for (final StackTraceElement element : trace) {
        traceBuilder.append("\tat ").append(element).append("\n");
    }//from  w  w w .  ja v  a  2  s  . c  om

    return traceBuilder.toString();
}

From source file:com.l2jfree.lang.L2Thread.java

public static List<String> getStats() {
    List<String> list = new FastList<String>();

    list.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date()));
    list.add("");
    list.add("## Java Platform Information ##");
    list.add("Java Runtime Name: " + System.getProperty("java.runtime.name"));
    list.add("Java Version: " + System.getProperty("java.version"));
    list.add("Java Class Version: " + System.getProperty("java.class.version"));
    list.add("");
    list.add("## Virtual Machine Information ##");
    list.add("VM Name: " + System.getProperty("java.vm.name"));
    list.add("VM Version: " + System.getProperty("java.vm.version"));
    list.add("VM Vendor: " + System.getProperty("java.vm.vendor"));
    list.add("VM Info: " + System.getProperty("java.vm.info"));
    list.add("");
    list.add("## OS Information ##");
    list.add("Name: " + System.getProperty("os.name"));
    list.add("Architeture: " + System.getProperty("os.arch"));
    list.add("Version: " + System.getProperty("os.version"));
    list.add("");
    list.add("## Runtime Information ##");
    list.add("CPU Count: " + Runtime.getRuntime().availableProcessors());
    list.add("");
    for (String line : getMemoryUsageStatistics())
        list.add(line);/*w w w . j av  a2s .c  o  m*/
    list.add("");
    list.add("## Class Path Information ##\n");
    for (String lib : System.getProperty("java.class.path").split(File.pathSeparator))
        if (!list.contains(lib))
            list.add(lib);
    list.add("");

    Set<Thread> threads = new TreeSet<Thread>(new Comparator<Thread>() {
        @Override
        public int compare(Thread t1, Thread t2) {
            if (t1.isDaemon() != t2.isDaemon())
                return Boolean.valueOf(t1.isDaemon()).compareTo(t2.isDaemon());

            final StackTraceElement[] st1 = t1.getStackTrace();
            final StackTraceElement[] st2 = t2.getStackTrace();

            for (int i = 1;; i++) {
                final int i1 = st1.length - i;
                final int i2 = st2.length - i;

                if (i1 < 0 || i2 < 0)
                    break;

                final int compare = st1[i1].toString().compareToIgnoreCase(st2[i2].toString());

                if (compare != 0)
                    return compare;
            }

            if (st1.length != st2.length)
                return Integer.valueOf(st1.length).compareTo(st2.length);

            return Long.valueOf(t1.getId()).compareTo(t2.getId());
        }
    });
    threads.addAll(Thread.getAllStackTraces().keySet());
    list.add("## " + threads.size() + " thread(s) ##");
    list.add("=================================================");

    int i = 1;
    for (Thread thread : threads) {
        list.add("");
        list.add(i++ + ".");
        list.addAll(getStats(thread));
    }

    return list;
}

From source file:Main.java

/**
 * Print a stack trace of the current thread.
 *//*from   w w w.  jav a 2 s . c  o m*/
public static void printFullStackTrace() {
    Thread thread = Thread.currentThread();
    System.out.printf("  Thread id: %d, name: %s, state: %s, daemon: %s, EDT: %s\n", thread.getId(),
            thread.getName(), thread.getState(), thread.isDaemon(), EventQueue.isDispatchThread());
    ThreadGroup group = thread.getThreadGroup();
    System.out.printf("    priority: %d, group: %s, group count: %d\n", thread.getPriority(), group.getName(),
            group.activeCount());
    StackTraceElement[] backtrace = thread.getStackTrace();

    for (StackTraceElement e : backtrace) {
        System.out.printf("    Stack Trace: %s\n", e);
    }
}

From source file:Main.java

/**
 * Print diagnostic info about the current thread.
 *//*ww w  .  j av a  2s .  c  om*/
public static void printThreadInfo() {
    Thread thread = Thread.currentThread();
    System.out.printf("  Thread id: %d, name: %s, state: %s, daemon: %s, EDT: %s\n", thread.getId(),
            thread.getName(), thread.getState(), thread.isDaemon(), EventQueue.isDispatchThread());
    ThreadGroup group = thread.getThreadGroup();
    System.out.printf("    priority: %d, group: %s, group count: %d\n", thread.getPriority(), group.getName(),
            group.activeCount());
    StackTraceElement[] backtrace = thread.getStackTrace();
    if (backtrace.length > 2) {
        System.out.printf("    trace[2]: %s\n", backtrace[2]);
    }
}

From source file:com.myJava.util.Util.java

public static void logThreadInformations(String header, Thread thread) {
    try {//from ww w  . java2s.  c o m
        StackTraceElement[] elements = thread.getStackTrace();
        String thd;
        if (header != null) {
            thd = header + "\n";
        } else {
            thd = "";
        }
        thd += "Thread dump :";
        if (elements != null) {
            for (int i = 0; i < elements.length; i++) {
                thd += "\n";
                if (i != 0) {
                    thd += "at ";
                }
                thd += elements[i].getClassName() + "." + elements[i].getMethodName() + " (Line "
                        + elements[i].getLineNumber() + ")";
            }
        }
        Logger.defaultLogger().fine(thd);
    } catch (Throwable e) {
        Logger.defaultLogger().warn(e.getMessage());
    }
}

From source file:com.kth.common.utils.etc.LogUtil.java

/**
 * Stack Trace  ?./*from  www  .j av a2 s  .c  o  m*/
 * 
 * @param clazz  ??  Class.
 * @param level  .
 * @param msg .
 */
public static void stackTrace(final Class<?> clazz, final int level, final String msg) {
    if (Log.isLoggable(TAG, level)) {
        Thread th = Thread.currentThread();
        StackTraceElement[] stack = th.getStackTrace();

        StringBuilder sb = new StringBuilder();
        if (msg != null && !"".equals(msg)) {
            sb.append(msg).append("\n");
        }
        for (StackTraceElement element : stack) {
            if (!"getStackTrace".equals(element.getMethodName())
                    && !"stackTrace".equals(element.getMethodName())) {
                sb.append("\tat ").append(element.toString()).append("\n");
            }
        }
        Log.println(level, TAG, LogUtil.getClassLineNumber(clazz) + " - " + sb.toString());
    }
}