假设我编写了一个简单的 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 上