Q. How to get the stack trace of a running c/c++ process on Unix.
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.