在gdb中如何将所有线程的堆栈输出到文件中去

1.先在gdb中设置log文件的位置,同时打开log

2.使用 thread apply all bt输出所有的堆栈

例子:

下面是gdb attach到httpd后的堆栈

(gdb) set logging file /tmp/test.txt
(gdb) set logging on
Copying output to /tmp/test.txt.
(gdb) thread apply all bt

Thread 2 (Thread 0x41ec5940 (LWP 7312)):
#0  0x00002b6370d241c0 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x00002aaab3bbf229 in ?? () from /usr/lib64/libnspr4.so
#2  0x00002aaab3bbfe69 in PR_WaitCondVar () from /usr/lib64/libnspr4.so
#3  0x00002aaab3bc51bc in PR_Sleep () from /usr/lib64/libnspr4.so
#4  0x00002aaab302750e in ?? () from /usr/lib64/libssl3.so
#5  0x00002aaab3bc55cd in ?? () from /usr/lib64/libnspr4.so
#6  0x00002b6370d1f77d in start_thread (arg=<value optimized out>) at pthread_create.c:301
#7  0x00002b637120d9ad in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x2b63731b4360 (LWP 4718)):
#0  0x00002b6371206b42 in select () from /lib64/libc.so.6
#1  0x00002b6370b10d25 in apr_sleep () from /usr/lib64/libapr-1.so.0
#2  0x00002b636f242315 in ap_wait_or_timeout ()
#3  0x00002b636f24b79e in ap_mpm_run ()
#4  0x00002b636f225fd8 in main ()

然后在/tmp/test.txt中可以自由的查找了。

refs:

http://stackoverflow.com/questions/18391808/getting-the-backtrace-for-all-
the-threads-in-gdb

http://www.delorie.com/gnu/docs/gdb/gdb_25.html

http://stackoverflow.com/questions/1707167/how-extract-text-from-gdb

下面这个连接有其它的debug技巧,比如如何debug X Error,

https://wiki.debian.org/HowToGetABacktrace