Viewing Java stack traces with jstack


I wrote yesterday about the jmap utility, which is a great utility for better understanding the arrangement of the JVM’s heap. Each thread that lives inside the JVM also contains an execution stack, which is used to store local variables and state information to allow function calls to work. The Java SDK /JRE comes with the jstack utility, which can be used to print the stack of each thread in human readable form:

$ /usr/java/bin/jstack pgrep java

Attaching to process ID 16498, please wait...
Debugger attached successfully.
Client compiler detected.
JVM version is 1.5.0_06-b05
Thread t@25: (state = BLOCKED)
- java.lang.Thread.sleep(long) @bci=721371649 (Interpreted frame)
- java.lang.Thread.sleep(long) @bci=0 (Interpreted frame)
- com.sun.patchpro.model.PatchProStateMachine$17.synchronize(com.sun.patchpro.util.StateMachine) @bci=30, line=828 (Interpreted frame)
- com.sun.patchpro.util.State.run() @bci=45, line=261 (Interpreted frame)
- java.lang.Thread.run() @bci=11, line=595 (Interpreted frame)


Thread t@23: (state = IN_NATIVE)
- java.lang.UNIXProcess.waitForProcessExit(int) @bci=0 (Interpreted frame)
- java.lang.UNIXProcess.waitForProcessExit(int) @bci=0 (Interpreted frame)
- java.lang.UNIXProcess.access$900(java.lang.UNIXProcess, int) @bci=2, line=17 (Interpreted frame)
- java.lang.UNIXProcess$2$1.run() @bci=17, line=86 (Interpreted frame)


Thread t@21: (state = BLOCKED)
- java.lang.Thread.sleep(long) @bci=144113 (Interpreted frame)
- java.lang.Thread.sleep(long) @bci=0 (Interpreted frame)
- com.sun.patchpro.plugins.sunos.pkg.SunOSBaseDataExtension.buildDatabase(java.io.InputStream) @bci=54, line=258 (Interpreted frame)
- com.sun.patchpro.plugins.sunos.pkg.SunOSBaseDataExtension.run() @bci=110, line=112 (Interpreted frame)
- java.lang.Thread.run() @bci=11, line=595 (Interpreted frame)


Thread t@17: (state = BLOCKED)
- java.lang.Object.wait(long) @bci=706035048 (Interpreted frame)
- java.lang.Object.wait(long) @bci=0 (Interpreted frame)
- java.lang.Thread.join(long) @bci=70, line=1103 (Interpreted frame)
- com.sun.patchpro.analysis.SunOSBaseData$DetectorThread.join(int) @bci=6, line=310 (Interpreted frame)
- com.sun.patchpro.analysis.SunOSBaseData.run() @bci=310, line=132 (Interpreted frame)
- java.lang.Thread.run() @bci=11, line=595 (Interpreted frame)

[ ..... ]

Each stack frame contains the state (e.g., runnable, blocked, etc) of the thread and a stack backtrace with the current stack frame displayed first. Good stuff!

This article was posted by Matty on 2006-05-21 10:25:00 -0400 -0400