Documenting Problems That Were Difficult To Find The Answer To

Dumping Stacks of Running Programs

This post is likely to be a work in progress as I collate information that is useful to me on the one post.

I find it incredibly useful to dump the stack of a running program. This is especially useful when a program “freezes” instead of crashes – because it helps you to isolate where the program has gone wrong. Perhaps it is a tight CPU loop (as a result of a poorly written while { } loop which didn’t update the conditional variable correctly) or the code is blocking on something such as I/O or, worst case scenario, a mutex that is held by another thread waiting for a mutex the other thread is holding (deadlock).


The jstack program is wonderful. You can give a process identifier (PID) and jstack will dump the stacks of all the threads in the Java application. I found this incredibly useful as a system administrator on a Java/Tomcat web estate – and solved deadlock issues as well as HTTP client throttling issues (one of the Apache HTTP classes defaulted to a maximum of 2 simultaneous connections to a single back end causing blocking).

Solaris/Linux C/C++


The pstack program is also wonderful. Give it a process identifier (PID) and it will do its best to verbalise the stack of the running process.

Apparently pstack exists for 32-bit Linux and on RedHat 64-bit. But no Ubuntu/Debian pstack for 64-bit.


One alternative on 64-bit Debian is to try that suggested in this link (found in this thread post):

gdb --q --n --ex bt --batch --pid PID

Linux /proc

Every process in Linux has a subdirectory in the /proc filing system with the path name /proc/<PID>/ (where PID is the process identifier number) and inside this directory is the stack file (i.e. /proc/<PID>/stack) which appears to contain any system calls if in the middle of such – or effectively nothing if in the middle of running user code.


Getting a Perl stack from a running Perl binary can be difficult.

cpan install App::Stacktrace
perl-stacktrace -m >plstack.gdb
gdb -p pid -x plstack.gdb



kill -QUIT pid

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: