How to get Java Call Stack of a running application
Solution 1
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.
Solution 2
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.
Solution 3
Have a look at Throwable.getStackTrace()
. Just create a new Throwable
; you don't actually have to throw
it.
Solution 4
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.
Related videos on Youtube
naeemgik
Senior Java Engineer | Java | J2EE | Spring | SOAP & REST | Microservices | Java Cryptography | Backend
Updated on July 26, 2022Comments
-
naeemgik almost 2 years
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
-
Michael about 11 yearsDuplicate of stackoverflow.com/questions/706292/…
-
-
naeemgik about 11 yearsI want to find complete call stack when i perform some action from the application button e.g. (on Register Patient). Do u know about BTrace what is it :(???
-
Brian Agnew about 11 yearsI can't really advise other than to refer you to an SO answer (see above)
-
Mikhail Vladimirov about 11 yearsThis will print stack trace for one thread only, the current thread.
-
Mikhail Vladimirov about 11 yearsThis will dump stack trace for one thread only, the current thread.
-
naeemgik about 11 yearsCan u please suggest me where to write this stackTrace() code snippet?? I want to write a single code snippet that can be used everywhere.
-
David Roussel about 11 yearsYes, the code line print the current thread. The other examples will print all thread stacks.
-
naeemgik about 11 yearsCan u please suggest me where to write this stackTrace() code snippet?? I want to write a single code snippet that can be used everywhere.
-
naeemgik about 11 yearsCan u please suggest me where to write this stackTrace() code snippet?? I want to write a single code snippet that can be used everywhere
-
John Leidegren over 9 years
jstack
does not ship as part of the JRE (at least not in version 7) it does however ship as part of the JDK.