Search code examples
c++gdb

GDB not breaking on main


I am having a very weird issue debugging with gdb. I have reduced it to the following minimal example.

Create test.cc, with the contents:

#include <iostream>

int main(void) {
    std::cout << "this is ridiculous" << std::endl;
}

and compile it using g++: g++ test.cc -g -Og -o out ./out works as expected, but gdb ./out behaves weird:

~ 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.

What am I doing wrong?

Things I have checked:

Symbols are there:

~ 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

The assembly output

pastebin link

Multiple Machines

Occurs on:

  • ubuntu 22.04 with g++ 11.4.0
  • arch with clang 17
  • arch with g++ 13.2.1

GDB parses symbols

~ 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

Edit: More Investigation

After continued investigation, I've noted the following:

  1. plain c programs also exhibit this behavior.
  2. the starti gdb command does not work, behaving like run
  3. when running in a debian docker container, everything works correctly

Point 3, to me, implies it is a bug in gdb, but that makes no sense, as:

  • it occurs on two different machines with two different versions of gdb (12.1 and 13.2).
  • gdb is installed from the main ubuntu/arch repos. I imagine that is one of the best-tested pieces of software out there

Are there more tests I can run to nail down the issue?


Solution

  • I have figured it out.

    this post contains the answer -- in short: gdb uses the shell environment variable to determine how to spawn the program at start. I am using a non-posix shell, which broke everything.