naeemgik
naeemgik

Reputation: 2272

How to get Java Call Stack of a running application

I am working on very huge java web based application. As there is no proper logging done while development so its very difficult for me to put break point and debug the app as i dont know execution order. Is there any mechanism to get complete Call Stack of the the running java application after I perform some actions.

I searched it on net for long time but cannot came to concrete solution. Please suggest me if something is there for it. Thanks

Upvotes: 18

Views: 25541

Answers (7)

Dennis van Beek
Dennis van Beek

Reputation: 59

Just in case people are reading this post to find out how to get the class or classes of the stack of the current thread:

StackTraceElement[] stack = Thread.currentThread().getStackTrace();
String smallStack = "Calling classes: " + System.lineSeparator() + stack[2] + System.lineSeparator() + stack[3];

This will get the stack for the current thread.

  • stack[0] will be the getStackTrace call itself.
  • stack[1] will be the line of code where you are working.
  • So stack[2] and stack[3] are the first 'interesting' stack elements.

Upvotes: 0

Graham Borland
Graham Borland

Reputation: 60681

Have a look at Throwable.getStackTrace(). Just create a new Throwable; you don't actually have to throw it.

Upvotes: 5

Mikhail Vladimirov
Mikhail Vladimirov

Reputation: 13890

Method 1: Use jstack utility from command line (part of the JDK distro).

Method 2: Send signal 3 to the java process, it will dump stack traces on stdout.

Method 3: Call Thread.getAllStackTraces () from within application:

public class StackTraceDumper
{
    public static dumpAllStackTraces ()
    {
        for (Map.Entry <Thread, StackTraceElement []> entry: 
            Thread.getAllStackTraces().entrySet ())
        {
            System.out.println (entry.getKey ().getName () + ":");
            for (StackTraceElement element: entry.getValue ())
                System.out.println ("\t" + element);
        }
    }
}

Then use StackTraceDumper.dumpAllStackTraces() where you need to dump stack traces.

Upvotes: 23

Amol U
Amol U

Reputation: 1

Why don't you use some AOP tool like AspectJ to capture these values and log? You can use execution() point cut along with after() advice. For non production deployment you can log all the method calls along with passed values and returned value. This will be too much overhead for production env. For that you can just store the passed values (Object args[] as you get in AspectJ advice) in local variable and only log it in case of exception. But even in that case there will be some performance penalty as primitive values will be boxed to be passed as Object[] to your advice.

Upvotes: 0

David Roussel
David Roussel

Reputation: 5916

There are some options:

  • Run it in a debugger, and pause all threads, then you can inspect them
  • Use VisualVM to connect to the running processes and trigger a thread dump. It comes with the JDK.
  • Use jstack on the command line, to dump threads.

Add some basic logging to your code:

new RuntimeException().printStackTrace();

That will pring a static trace to stderr

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272207

You can trigger a stack dump via pressing Ctrl+Break, or sending a signal 3 (on Unix-based systems). Note that you'll get a stack trace per-thread. This will go to standard error, so make sure your logging is capturing this.

You can do this programatically via

Map<Thread, StackTraceElement[]> m = Thread.getAllStackTraces();

Here's more info on getting and analysing stack traces.

As you've noted, BTrace is another possibility. Here's an SO answer on using it.

Upvotes: 3

Dewfy
Dewfy

Reputation: 23614

Thread.dumpStack() Prints a stack trace of the current thread to the standard error stream. Thread.getAllStackTraces() Returns a map of stack traces for all live threads. Thread.getStackTrace() Returns an array of stack trace elements representing the stack dump of this thread.

Upvotes: 6

Related Questions