MacOS – Mysterious GDB hangs after clean & build

developmentmacosmojave

OS: Mac OSX 10.14.6
GDB version: 8.2.50.20190226-git
g++ version: 11.0.0 (clang-1100.0.33.16)

The Problem: Whenever I clean and rebuild my simple "Hello World" c++ application (using g++) and then try to debug it using gdb, gdb will hang indefinitely. But the weirder part comes next: if I go to the activity monitor and kill the running gdb process, then run gdb again (killing it if it hangs again), eventually gdb will work and I'll be able to step through my code. I've followed the steps described here to properly code-sign gdb, so that doesn't appear to be the problem. Mac Experts, any idea why rebuilding a C++ executable would prevent GDB from being able to debug it until GDB has been run & killed a few times?

For reference, here are the raw terminal commands that the Makefile generates:

$ rm -f -r ./build/Debug
$ rm -rf ./dist/Debug
$ mkdir -p ./build/Debug/GNU-MacOSX
$ mkdir -p ./dist/Debug/GNU-MacOSX
$ g++    -c -g -MMD -MP -MF "build/Debug/GNU-MacOSX/welcome.o.d" -o build/Debug/GNU-MacOSX/welcome.o welcome.cc
$ g++     -o dist/Debug/GNU-MacOSX/welcome_1 build/Debug/GNU-MacOSX/welcome.o

And here's what happens when I run gdb:

Starting program: /Users/matthewboyle/NetBeansProjects/Welcome_1/dist/Debug/GNU-MacOSX/welcome_1 
[New Thread 0xf03 of process 33622]
[New Thread 0xd03 of process 33622]
(It just hangs after this point, I have to kill the process)

Best Answer

I ran into the same problem and was able to resolve it by compiling with GNU g++ and including level 3 GDB debugging info.

$ g++-10 -ggdb3 FILE.cpp

Addendum: adding my toolchain versions for posterity.

$ uname -a
Darwin manco.local 19.6.0 Darwin Kernel Version 19.6.0: Thu Oct 29 22:56:45 PDT 2020; root:xnu-6153.141.2.2~1/RELEASE_X86_64 x86_64 i386 MacBookPro15,2 Darwin

$ g++-10 --version
g++-10 (Homebrew GCC 10.2.0) 10.2.0
...

$ gdb --version
GNU gdb (GDB) 10.1
...