8

I have built GDB from source on macOS 12.0 and codesigned it. However, every time I try to debug a program, I get this:

(gdb) b main Breakpoint 1 at 0x10000324f: file main.cpp, line 50. (gdb) run Starting program: /Users/fluzzlesnuff/Documents/C++/a.out [New Thread 0x2a03 of process 2389] 

and then GDB hangs. I have been compiling C++ programs with g++. Here are some things I have tried:

  1. Compile with -g option.
  2. Compile with -ggdb option.
  3. Compile with -ggdb3 option.
  4. Run with sudo
  5. Kill and re-run gdb repeatedly.
  6. Try different binaries.
  7. Disable SIP
  8. Add more breakpoints.
  9. set startup-with-shell off in .gdbinit

I do not get taskgated / Mach Port errors; only the New Thread message.

I realize that this is a near-duplicate of this, but I didn't see a working answer on that question.

For reference, here are my versions:

$ g++ --version Apple clang version 12.0.0 (clang-1200.0.32.29) Target: x86_64-apple-darwin21.0.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin 
$ gdb --version GNU gdb (GDB) 10.2 
6
  • As you are using clang it might be a better idea to use lldb (as used in Xcode) as the debugger. I think gdb is more aligned to use with GNU cc which your g++ is not Commented Oct 31, 2021 at 14:51
  • g++ is an alias of gcc with some C++ options, so it’s definitely GNU. gcc.gnu.org Commented Nov 1, 2021 at 15:27
  • That is not what you show in the question the result of g++ --version says it isclang Commented Nov 1, 2021 at 22:15
  • I was kind of wondering about that. I just learned that g++ is really clang++ in disguise. This is likely the source of my problems. Commented Nov 2, 2021 at 4:10
  • 1
    If you're amenable to using lldb you can sidestep this altogether and have a working debugger. I wasn't able to get gdb working and hit this same error. No such issue with lldb which provides very similar functionality. Commented May 6, 2022 at 5:54

3 Answers 3

2

As usual, attention to detail was the issue. I saw that g++ --version reported clang, but I didn’t think about it enough. I had assumed that GDB was the problem, not my compiler. I’m not sure why Apple linked g++ to clang++, since that’s quite misleading. This little oversight made me spend 8 months writing C++ on my phone (vi on a 60-character wide display is not my ideal IDE).

What I did to get real g++:

  1. Install gcc with Homebrew

  2. Soft-link (ln -s) gcc-11 and g++-11 from /usr/local/Cellar/ to /usr/local/bin/

  3. unhash (at least in zsh) gcc and g++, or else gcc will continue to expand to /usr/bin/clang

GDB is working great now.

6
  • Why do you need gnu c++? Clang does most things as well and for macos and ios it is probably better. With clang you can use xcode which is a lot better ide than vi on an iPhone. Also doing most work in c you do not call gcc or clang on the command line but a build system eg make and for hat you want to se CC to the full path and don't rely on your $PATH Commented Nov 3, 2021 at 10:35
  • I’m not able to debug programs compiled with clang using gdb. I tried using Xcode, but it was much more than I needed for writing small utilities. I’m writing command-line utilities, so I find it easiest to g++ *.cpp, then run a.out with whatever arguments I desire. Commented Nov 3, 2021 at 14:44
  • Then use lldb to debug Commented Feb 23, 2022 at 8:55
  • I am running into same issue despite following above steps. I am using GDB version 2.1 and using MacOS Monterey 12.6.1 Commented Dec 26, 2022 at 0:28
  • @TusharJadhav GDB 2.1 is very old; is there any reason you need to use such an old version? Commented Dec 27, 2022 at 2:49
1

EDIT: It seems that I was wrong, it still only works sporadically, sometimes it hangs sometimes it doesn't. It just happened to work three times in a row after running this command :(

I had the same problem and fought for hours trying to install gdb on mac. Found this blog that said that I should not have run

sudo DevToolsSecurity -enable 

which I never did. So I tried it and it worked. Another possible solution was to run gdb with sudo every time but I felt this was better. No idea why they worked but they did, I hope someone else finds this useful.

1
  • I didn’t run this, but after a recent update to the Command Line Developer Tools, I was asked for Touch-ID confirmation when I started up gdb. That makes me think your command, or some form of it, was run during the update, and now does a security check when running developer tools. Commented Nov 2, 2021 at 4:41
1

This hang hits gdb bug: https://sourceware.org/bugzilla/show_bug.cgi?id=24069

You can install gdb from this source with patch:

brew install --force --build-from-source domq/gdb/gdb # a patched version based on 10.1 

or you can

brew update brew install gdb # use gdb 12.1 

It works for me, and I found it from this link: https://gist.github.com/mike-myers-tob/9a6013124bad7ff074d3297db2c98247

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.