GDB: Get stack trace of all threads of running process

Q. How to get the stack trace of a running c/c++ process on Unix.

Ans:

1. Attach GDB to a running process:

Get the pid of running process foo. $ps -aef | grep foo
Attach gdb to the pid of process foo
$sudo gdb -p 21884

2. Save the stack trace of all running threads in a log file.

(gdb) set pagination off
(gdb) set logging on
 Copying output to gdb.txt.
(gdb) thread apply all bt

3. Detach gdb from running process

(gdb) detach
 Detaching from program: foo, process 21884

After this you’ll find the stack trace of all the threads of your running process in file text gdb.txt This is extremely useful to debug the hung process.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s