如何解决为什么即使执行C ++源文件不包含任何功能,“ gdb”在执行“启动”命令后仍列出多个功能?
上下文
考虑以下文件
$ cat main.cpp
int main() {return 0;}
我可以通过执行列出所有可用功能
$ g++ -g main.cpp && gdb -q -batch -ex 'info functions -n' a.out
All defined functions:
File main.cpp:
1: int main();
在执行start
之前执行info functions
时,列出了1000多个功能(见下文)
g++ -g main.cpp && \
gdb -q -batch -ex 'start' -ex 'info functions -n' a.out | \
head -n 10
Temporary breakpoint 1 at 0x111d: file main.cpp,line 1.
Temporary breakpoint 1,main () at main.cpp:1
1 int main() {return 0;}
All defined functions:
File /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/allocated_ptr.h:
70: void std::__allocated_ptr<std::allocator<std::_Sp_counted_ptr_inplace<std::filesystem::__cxx11::filesystem_error::_Impl,std::allocator<std::filesystem::__cxx11::filesystem_error::_Impl>,(__gnu_cxx::_Lock_policy)2> > >::~__allocated_ptr();
70: void std::__allocated_ptr<std::allocator<std::_Sp_counted_ptr_inplace<std::filesystem::filesystem_error::_Impl,std::allocator<std::filesystem::filesystem_error::_Impl>,(__gnu_cxx::_Lock_policy)2> > >::~__allocated_ptr();
如下所示,打印的总行数如此,显然列出了1000多个功能
g++ -g main.cpp && gdb -q -batch -ex 'start' -ex 'info functions -n' a.out | wc -l
4436
问题
从上面我们可以看到,main.cpp
文件不包含任何功能,为什么gdb
列出了在执行start
命令之前而不是在{{ 1}}尚未执行?
其他上下文
正如该问题的评论之一所建议的,这是在执行start
之后执行info shared
的输出
start
g++ -g main.cpp && gdb -q -batch -ex 'start' -ex 'info shared' a.out
解决方法
main.cpp文件不包含任何功能,那么gdb为什么在之前执行了start命令但未执行start时列出这些功能?
在start
之前,GDB仅读取主可执行文件的符号(和调试信息)。
在start
之后,动态链接的可执行文件将加载共享库(在info shared
中可见),而GDB(默认情况下)将读取它们的符号表和调试信息。而且由于这些库包含数百个函数,因此GDB知道所有这些函数。
您可以使用set auto-solib-add off
来防止这种情况,但是通常您不想这样做。如果这样做,您的程序就会崩溃,例如abort
,除非您使用sharedlibrary
或add-symbol-file
command手动添加符号,否则GDB将不会知道您崩溃的位置 。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。