C++ 为什么是;gdb";在执行";启动';即使在C++;源文件没有';不包含任何函数? 上下文
考虑以下文件C++ 为什么是;gdb";在执行";启动';即使在C++;源文件没有';不包含任何函数? 上下文,c++,gdb,C++,Gdb,考虑以下文件 $ cat main.cpp int main() {return 0;} 我可以通过执行 $ g++ -g main.cpp && gdb -q -batch -ex 'info functions -n' a.out 在执行info函数之前执行start时,会列出1000多个函数(见下文) 问题 正如我们在上面所看到的,main.cpp文件不包含任何函数,那么为什么gdb在以前执行过start命令但没有执行start时会列出这些函数呢 附加上下文 正如在这个
$ cat main.cpp
int main() {return 0;}
我可以通过执行
$ g++ -g main.cpp && gdb -q -batch -ex 'info functions -n' a.out
在执行info函数之前执行start
时,会列出1000多个函数(见下文)
问题
正如我们在上面所看到的,main.cpp
文件不包含任何函数,那么为什么gdb
在以前执行过start
命令但没有执行start
时会列出这些函数呢
附加上下文
正如在这个问题的一个评论中所建议的,这里是执行start
后执行info shared
的输出
g++ -g main.cpp && gdb -q -batch -ex 'start' -ex 'info shared' a.out
main.cpp文件不包含任何函数,那么为什么gdb在以前执行过start命令但没有执行start命令时会列出这些函数呢
在启动之前,GDB只读取主可执行文件的符号(和调试信息)
在start
之后,动态链接的可执行文件加载共享库(参见info shared
),GDB(默认情况下)读取每个库的符号表和调试信息。由于这些库包含数百个函数,GDB知道所有这些函数
您可以通过设置auto solib add off来防止这种情况,但通常您不想这样做。如果您这样做了,并且您的程序崩溃,例如,中止
,GDB将不知道您在哪里崩溃,除非您使用共享库
或添加符号文件
手动将符号添加回标准库中定义的函数,默认情况下,哪个链接到您的程序?在运行start
之后,您还可以运行info shared
并向我们显示输出吗?@MarkPlotnick问题中添加了info shared
命令的输出。这是一种常见的解决方法,因为您使用的是-n
选项,它告诉gdb只显示在debuginfo中找到的函数:在启动程序之前,您可以通过执行类似于set debug file directory/nonexistent
的操作,使gdb无法找到标准共享库的debuginfo文件。(在CentOS上测试。)
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();
g++ -g main.cpp && gdb -q -batch -ex 'start' -ex 'info functions -n' a.out | wc -l
4436
g++ -g main.cpp && gdb -q -batch -ex 'start' -ex 'info shared' a.out
Temporary breakpoint 1 at 0x111d: file main.cpp, line 1.
Temporary breakpoint 1, main () at main.cpp:1
1 int main() {return 0;}
From To Syms Read Shared Object Library
0x00007ffff7fd2090 0x00007ffff7ff2746 Yes (*) /lib64/ld-linux-x86-64.so.2
0x00007ffff7e4c040 0x00007ffff7f37b52 Yes /usr/lib/libstdc++.so.6
0x00007ffff7c7f3b0 0x00007ffff7d1a658 Yes (*) /usr/lib/libm.so.6
0x00007ffff7c59020 0x00007ffff7c69ca5 Yes /usr/lib/libgcc_s.so.1
0x00007ffff7ab3650 0x00007ffff7bfe6bd Yes (*) /usr/lib/libc.so.6
(*): Shared library is missing debugging information.