我在使用 gdb 进行调试时遇到了一个非常奇怪的问题。我已将其简化为以下最小示例。
创建
test.cc
,内容为:
#include <iostream>
int main(void) {
std::cout << "this is ridiculous" << std::endl;
}
并使用
g++
进行编译: g++ test.cc -g -Og -o out
./out
按预期工作,但 gdb ./out
表现得很奇怪:
~ gdb ./out
Reading symbols from ./out...
(gdb) b main
Breakpoint 1 at 0x11df: file test.cc, line 3.
(gdb) r
Starting program: /home/c/tmp/s/out
this is ridiculous
During startup program exited normally.
我做错了什么?
~ objdump -x out | grep main
000000000000120c l F .text 000000000000001c _GLOBAL__sub_I_main
00000000000011df g F .text 000000000000002d main
0000000000000000 F *UND* 0000000000000000 __libc_start_main@GLIBC_2.34
发生于:
ubuntu 22.04 with g++ 11.4.0
arch with clang 17
arch with g++ 13.2.1
~ gdb ./out
(gdb) info functions
All defined functions:
File test.cc:
3: int main();
static void _GLOBAL__sub_I_main();
static void __static_initialization_and_destruction_0(int, int);
Non-debugging symbols:
0x0000000000001000 _init
0x0000000000001070 __cxa_finalize@plt
0x0000000000001080 std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)@plt
0x0000000000001090 __cxa_atexit@plt
0x00000000000010a0 std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)@plt
0x00000000000010b0 std::ios_base::Init::Init()@plt
0x00000000000010c0 _start
0x00000000000010f0 deregister_tm_clones
0x0000000000001120 register_tm_clones
0x0000000000001160 __do_global_dtors_aux
0x00000000000011a0 frame_dummy
0x0000000000001228 _fini
经过持续调查,我注意到以下几点:
c
程序也表现出这种行为。starti
gdb 命令不起作用,其行为类似于 run
第 3 点,对我来说,意味着这是 gdb 中的一个错误,但这没有意义,因为:
我可以运行更多测试来确定问题吗?