为什么 "gdb" 即使 C++ 源文件不包含任何函数,执行“开始”命令后也会列出多个函数?

Why is "gdb" listing multiple functions after executing the "start' command even when the C++ source file doesn't contain any function?

上下文

考虑以下文件

$ 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();

在执行 info functions 之前执行 start 时列出了 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 命令之前已经执行过,而当start还没执行?

其他上下文

正如该问题的其中一条评论所建议的,这是在执行 start 之后执行 info shared 的输出

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.

main.cpp file does not contain any function, so why is gdb listing those functions when the start command has been executed before but not when start hasn't been executed?

start 之前,GDB 只读取主要可执行文件的符号(和调试信息)。

start 之后,动态链接的可执行文件加载共享库(在 info shared 中看到),GDB(默认情况下)读取每个共享库的符号表和调试信息。由于这些库包含数百个函数,GDB 知道所有这些函数。

您可以使用 set auto-solib-add off 来阻止这种情况,但通常您不想这样做。如果你这样做,你的程序就会崩溃,例如abort,GDB 不会知道你崩溃的地方 除非 你使用 sharedlibraryadd-symbol-file command.[= 手动添加符号。 19=]