在gdb中重新編譯後重新載入符號表

在gdb中重新編譯後重新載入符號表

假設我寫了一個簡單的 C++ 程式

#include <stdexcept>

int main(int argc, char *argv[])
{
  throw std::logic_error("Error");
  return 0;
}

這個程式(用 編譯g++ -O0 -ggdb -o test test.cpp)不出所料地拋出一個由 gdb 捕捉的異常:

> gdb test
Reading symbols from test...
(gdb) r
Starting program: /home/christoph/test/gdb/test 
terminate called after throwing an instance of 'std::logic_error'
  what():  Error

Program received signal SIGABRT, Aborted.
0x00007ffff7abace5 in raise () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007ffff7abace5 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff7aa4857 in abort () from /usr/lib/libc.so.6
#2  0x00007ffff7e3b81d in __gnu_cxx::__verbose_terminate_handler () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007ffff7e482ea in __cxxabiv1::__terminate (handler=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:48
#4  0x00007ffff7e48357 in std::terminate () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:58
#5  0x00007ffff7e485be in __cxxabiv1::__cxa_throw (obj=<optimized out>, tinfo=0x555555557da8 <typeinfo for std::logic_error@@GLIBCXX_3.4>, dest=0x7ffff7e5e660 <std::logic_error::~logic_error()>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:95
#6  0x00005555555551c0 in main (argc=1, argv=0x7fffffffdf08) at test.cpp:5

假設我有一個編譯程序,我在持續的 gdb 會話期間 ing 原始碼Makefile後從 gdb 執行該程式:touch

(gdb) make
g++ -O0 -ggdb -o test test.cpp

編譯成功,但是當我再次執行程式時,出現以下情況:

(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
`/dir/to/test' has changed; re-reading symbols.
Starting program: /dir/to/test 
terminate called after throwing an instance of 'std::logic_error'
  what():  Error

Program received signal SIGABRT, Aborted.
0x00007ffff7abace5 in ?? ()
(gdb) bt
#0  0x00007ffff7abace5 in ?? ()
#1  0x0000000000000000 in ?? ()

顯然,符號沒有被正確地重新讀取,堆疊追蹤是無用的。如果我重新讀取符號表,那麼一切都會按預期工作:

(gdb) symbol-file  /dir/to/test
Reading symbols from /dir/to/test...
Reading symbols from /usr/lib/libstdc++.so.6...
Reading symbols from /usr/lib/libm.so.6...
(No debugging symbols found in /usr/lib/libm.so.6)
Reading symbols from /usr/lib/libgcc_s.so.1...
Reading symbols from /usr/lib/libc.so.6...
(No debugging symbols found in /usr/lib/libc.so.6)
Reading symbols from /lib64/ld-linux-x86-64.so.2...
(No debugging symbols found in /lib64/ld-linux-x86-64.so.2)
(gdb) bt
#0  0x00007ffff7abace5 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff7aa4857 in abort () from /usr/lib/libc.so.6
#2  0x00007ffff7e3b81d in __gnu_cxx::__verbose_terminate_handler () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007ffff7e482ea in __cxxabiv1::__terminate (handler=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:48
#4  0x00007ffff7e48357 in std::terminate () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:58
#5  0x00007ffff7e485be in __cxxabiv1::__cxa_throw (obj=<optimized out>, tinfo=0x555555557da8 <typeinfo for std::logic_error@@GLIBCXX_3.4>, dest=0x7ffff7e5e660 <std::logic_error::~logic_error()>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:95
#6  0x00005555555551c0 in main (argc=1, argv=0x7fffffffdf08) at test.cpp:5

這是 gdb 中的錯誤還是預期行為?是否有一些開關可以自動重新載入符號表?

注意:該行為發生在 gdb 9.1 和 g++ 9.2.1 上

相關內容