为什么"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?
上下文
考虑以下文件
$ 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将不知道你在哪里崩溃了,除非你使用sharedlibrary
或add-symbol-file
命令手动添加回符号。
相关文章:
- 使用多个源文件时对类成员函数的未定义引用
- C++,是否有可能/如何定义在.h和.cpp源文件中调用函数的类构造函数
- 如何在源文件中定义模板函数
- "<某些系统标头>:错误:"<在此处插入函数>"在移动源文件后不是"std"的成员
- 获取对源文件中特定函数的所有调用并生成其他文件(使用 C、C++预处理器或脚本)
- 创建 Matlab MEX 函数时,我是否将 mexFunction 放在 c++ 头文件或源文件中
- 函数跨多个C 源文件
- C++编译器在一个源文件中的一个函数调用中引发错误,但在具有相同函数调用的另一个源文件中不会引发错误
- 如何创建用于多个源文件的全局变量/包含/函数
- 将函数放在标题与C 源文件中的位置
- 如何在头文件和源文件中组织模板函数和函数
- 从C源文件调用C++源文件中定义的全局函数
- 如何在源文件中实现嵌套类构造函数
- C++ 将函数放在单独的源文件中
- C++单独编译,不同源文件使用相同函数
- 编译一个源文件,其中包含在标头中声明的函数
- 在C++程序或静态类中具有函数的C源文件
- 别名不同源文件中的函数
- 使用全局变量作为在其他源文件中分配的函数的参数
- 头文件与源文件中的extern函数