2015年3月6日 星期五

[Embedded] 使用 gdb 時,程式因為收到 SIGTRAP 而結束的解決方法

問題描述:使用 gdb 追蹤程式時,程式因為收到 SIGTRAP 而結束。

問題現象:執行程式時,gdb 與 gdbserver分別出現下列 warning,接著當程式執行到 breakpoint 時,gdb便自行結束。

1. gdb server warning
gdbserver: error initializing thread_db library: version mismatch between libthread_db and libpthread 
2. gdb client warning
Attached to process 16523
Error while mapping shared library sections:
/lib/libbus.so: No such file or directory.
warning: .dynamic section for "/lib/libpthread.so.0" is not at the expected address (wrong library or version mismatch?)
warning: .dynamic section for "/lib/libm. 
3. when break point is meet
Child terminated with signal = 0x5 (SIGTRAP)
[2]+  Trace/breakpoint trap      /usr/local/bin/xxxx


問題原因:很明顯是因為 library 版本不符,摘錄 gdb FAQ 如下:
10. GDB does not see any threads besides the one in which crash occurred; or SIGTRAP kills my program when I set a breakpoint.
  • This frequently happen on Linux, especially on embedded targets. There are two common causes:
    • you are using glibc, and you have stripped libpthread.so.0
    • mismatch between libpthread.so.0 and libthread_db.so.1
    GDB itself does not know how to decode "thread control blocks" maintained by glibc and considered to be glibc private implementation detail. It useslibthread_db.so.1 (part of glibc) to help it do so. Therefore, libthread_db.so.1 and libpthread.so.0 must match in version and compilation flags. In addition,libthread_db.so.1 requires certain non-global symbols to be present in libpthread.so.0Solution: use strip --strip-debug libpthread.so.0 instead ofstrip libpthread.so.0.

解決方法:重新編譯 gdb, 確認 library version 一致即可,如下
a. download latest gdb
wget http://ftp.gnu.org/gnu/gdb/gdb-7.9.tar.gz>  
b. configure
For GDB, do 'configure --target=arm-none-linux-gnueabi'. Run the toplevel configure for that (/path/to/gdb-src/configure). 
For GDBserver do 'configure --host=arm-none-linux-gnueabi'. Run GDBserver's configure for that (/path/to/gdb-src/gdb/gdbserver/configure). 
c. make
Run the toplevel configure for that (/path/to/gdb-src/make)
參考資料