1

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Carson
  • 2,700
  • 11
  • 24
  • Did you try dropping the `-O` option entirely and compiling without it? – Sam Varshavchik Aug 30 '23 at 16:57
  • @SamVarshavchik yes, still fails – Carson Aug 30 '23 at 16:59
  • why not use CPPFLAGS and make? How does that even compile when you don't return a value from main? – UpAndAdam Aug 30 '23 at 17:18
  • @UpAndAdam this originally occured using the meson build system. I do not understand the suggestion of `make`, as it is just calling `g++` like I do. Adding `return 0;` at the end does not change the behavior – Carson Aug 30 '23 at 17:23
  • @UpAndAdam Please see [basic.start.main#5](https://eel.is/c++draft/basic.start.main#5): `"If control flows off the end of the compound-statement of main, the effect is equivalent to a return with operand 0"`. – G.M. Aug 30 '23 at 17:32
  • @G.M. if you are going to throw a language lawyer quote at me, use something that doesnt say 'draft' from website I've never heard of. the draft even says " Note: this is an early draft. It's known to be incomplet and incorrekt, and it has lots of bad formatting." OP: I get it you were simplifying out meson for us and showing us the actual commands executed Very fair point. – UpAndAdam Aug 30 '23 at 17:41
  • @G.M. that said i found other posts that indicate you are correct. that is news to me as I've always gotten errors/warnings when it has been omitted. https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – UpAndAdam Aug 30 '23 at 17:47
  • 1
    On ubuntu 20.04, what happens if you use the version of GDB that comes with it (9.2)? – Mark Plotnick Aug 30 '23 at 18:36
  • @MarkPlotnick I mistakenly indicated 20.04, I meant 22.04. Currently getting an old version of gdb to test with – Carson Aug 30 '23 at 18:58

1 Answers1

3

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.

Carson
  • 2,700
  • 11
  • 24