为什么"gdb"在执行"start"命令后列出多个函数,即使C++源文件不包含任何函数?

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

本文关键字:函数 源文件 即使 包含任 C++ 何函数 start 执行 gdb 命令 为什么      更新时间:2024-05-09

上下文

考虑以下文件

$ 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文件不包含任何函数,那么为什么gdb在之前执行过启动命令时列出这些函数,而在没有执行启动命令时不列出?

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

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

您可以使用set auto-solib-add off来防止这种情况,但通常您不想这样做。如果你这样做了,并且你的程序在例如abort中崩溃,GDB将不知道你在哪里崩溃了,除非你使用sharedlibraryadd-symbol-file命令手动添加回符号。