g++ 段错误堆栈查找

如果错误不是在动态库中
$ g++ test.cpp -g -o test
$ ./test
Obtained 6 stack frames.nm
./test() [0x400787]
/lib/x86_64-linux-gnu/libc.so.6(+0x354b0) [0x7f2430a5b4b0]
./test() [0x400876]
./test() [0x4008ea]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7f2430a46830]
./test() [0x400659]


$ addr2line -e test 0x400876


如果错误出在动态库中(动态库编译时需要加-g或者-rdynamic)

$ gcc test22.cpp -g -rdynamic -shared -fPIC -o libtest22.so
$ gcc test2.cpp -L. -ltest22 -g -rdynamic -Wl,--rpath=. -o test2
$ ./test2
Obtained 6 stack frames.nm
./test2() [0x400937]
/lib/x86_64-linux-gnu/libc.so.6(+0x354b0) [0x7f25350e54b0]
./libtest22.so(_Z6func_cv+0x9) [0x7f253547a659]
./test2() [0x400a8b]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7f25350d0830]
./test2() [0x400809]
$ # get the address in the ELF so using objdump or nm
$ nm libtst.so | grep func
0000000000000650 T _Z6func_cv
$ # get the (hex) address after adding the offset 
$ # from the start of the symbol (as provided by backtrace_syms())
$ python -c 'print hex(0x0650+0x9)'
0x659
$ # use addr2line to get the line information, assuming any is available            
$ addr2line -e libtest22.so 0x659

  如果.so 是由.o编译来的

#libvoiceInterface.so 是由多个.o编译成的,其中一个是ResponseTulingOnline.o,运行时其中get_tuling_response_text()中产生了堆栈错误
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fc98b9f37e5] /lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x5c)[0x7fc98ba9515c] /lib/x86_64-linux-gnu/libc.so.6(+0x117160)[0x7fc98ba93160] /lib/x86_64-linux-gnu/libc.so.6(+0x1166c9)[0x7fc98ba926c9] /lib/x86_64-linux-gnu/libc.so.6(_IO_default_xsputn+0x80)[0x7fc98b9f76b0] /lib/x86_64-linux-gnu/libc.so.6(_IO_vfprintf+0x139b)[0x7fc98b9ca50b] /lib/x86_64-linux-gnu/libc.so.6(__vsprintf_chk+0x84)[0x7fc98ba92754] /lib/x86_64-linux-gnu/libc.so.6(__sprintf_chk+0x7d)[0x7fc98ba926ad] ../lib/libvoiceInterface.so(_ZN20ResponseTulingOnline24get_tuling_response_textEPKaRPa+0x712)[0x7fc98c5f6592] ../lib/libvoiceInterface.so(_ZN20ResponseTulingOnline8responseEP10ASR_resultPaRi+0x55)[0x7fc98c5f6ae5] ../lib/libvoiceInterface.so(response_tuling+0x61)[0x7fc98c605621] ../lib/libvoiceInterface.so(response+0x6fc)[0x7fc98c60668c] ./bin/demo_VI(main+0x1f4)[0x40185a] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fc98b99c830] ./bin/demo_VI[0x401599]
......

#这时,查出错位置时应该在.o中找
$ nm ResponseTulingOnline |grep get_tuling_response_text
00000000000012e0 T _ZN20ResponseTulingOnline24get_tuling_response_textEPKaRPa
#接下来同上,根据出错位置在get_tuling_response_text方法中的偏移计算在ResponseTulingOnline中的偏移
$ python -c "print hex(0x12e0+0x712)"
0x19f2
$ addr2line -e build/ResponseTulingOnline.o 0x19f2
# 这里应该能找到出错代码行了(这里?是因为这段是分2次写的,中间时间有点长,第一次编译完后代码已经被我改了,且再次编译过,所以偏移位置变了,前后不一致,具体结果没有去验证,应该是没错的)
ResponseTulingOnline.cpp:?

参考:

  https://stackoverflow.com/questions/7556045/how-to-map-function-address-to-function-in-so-files




原文地址:https://www.cnblogs.com/dyan1024/p/9347422.html