47

I have installed gdb 8.1 with brew.

I have codesign gdb also and .gdbinit as below:

set startup-with-shell off.

I have disabled SIP feature:

$ csrutil status
System Integrity Protection status: disabled.

But gdb still doesn't work:

#include <iostream>
using namespace std;

int main() {
  cout << "hello world!" << endl;
  return 0;
}

Compile command:

g++ -g test.cpp

gdb output:

GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin17.3.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...Reading symbols from /Users/mypc/Downloads/a.out.dSYM/Contents/Resources/DWARF/a.out...done.
done.
(gdb) run
Starting program: /Users/mypc/Downloads/a.out 
[New Thread 0x2503 of process 802]
[New Thread 0x2303 of process 802]
During startup program terminated with signal ?, Unknown signal.
(gdb) 

What correct steps to make gdb work on macos sierra?

lucky1928
  • 8,708
  • 10
  • 43
  • 92
  • Seems gdb is broken on macos, see https://github.com/Homebrew/homebrew-core/issues/5912 and https://sourceware.org/bugzilla/show_bug.cgi?id=20266. – ks1322 Mar 02 '18 at 16:25
  • Hey, I had the same problem. What steps did you take to solve this? Did you ever receive this error when you ran a program: `Unable to find Mach task port for process-id 62995: (os/kern) failure (0x5). (please check gdb is codesigned - see taskgated(8))`? – nullbyte Mar 06 '18 at 16:09
  • @nullbyte see below answer, download gdb to 8.0.1 works fine. – lucky1928 Mar 06 '18 at 18:14
  • @nullbyte that's actually a separate, unrelated issue. [See my question](https://stackoverflow.com/questions/49184931/subject-cannot-codesign-system-certificate-for-gdb-in-keychain-access-in-mac-os/49246744#49246744). – xdavidliu Mar 13 '18 at 01:29
  • 1
    @xdavidliu this is actually a very related issue. – nullbyte Mar 13 '18 at 02:19
  • @nullbyte a complaint about mach ports means the codesigning wasn't done correctly. That's an issue with keychain access in mac os. The error message for *this* question, on the other hand, is regarding [a breakpoint trap with two threads](https://stackoverflow.com/questions/9809413/program-received-signal-sigtrap-trace-breakpoint-trap). That is unrelated to codesigning. Even if you did `sudo gdb` so that you never see any complaints about mach ports, you would still get this issue of breakpoint trap with two threads, assuming that you're using the latest brew gdb 8.1 instead of 8.0.1 – xdavidliu Mar 13 '18 at 02:22
  • btw, I'm getting `During startup program terminated with signal SIGTRAP, Trace/breakpoint trap.` instead under the two lines of `[New Thread 0x____ of process ____]`, but it's clearly the same issue. – xdavidliu Mar 13 '18 at 02:29
  • update: message in my comment above occurs with `(gdb) run`. Message in OP's question occurs with `(gdb) start`. This is almost certainly the same issue. – xdavidliu Mar 13 '18 at 19:27
  • @nullbyte, take a look on this official tutorial: https://sourceware.org/gdb/wiki/PermissionsDarwin – borgomeister Apr 23 '21 at 16:18
  • I believe that it is this bug that causes this error message: https://sourceware.org/bugzilla/show_bug.cgi?id=24069. Codesigning is a prerequisite for even getting this far. – thoni56 Oct 12 '21 at 12:23

3 Answers3

62

This is caused by latest gdb 8.1, downgrade gdb to 8.0.1 could solve this problem.

How to downgrade to gdb 8.0.1

  • Unlink current gdb: brew unlink gdb
  • Install gdb 8.0.1: brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/9ec9fb27a33698fc7636afce5c1c16787e9ce3f3/Formula/gdb.rb
  • Optional: avoid upgrade gdb with brew pin gdb
Felipe Lima
  • 10,530
  • 4
  • 41
  • 39
Hongbo Liu
  • 2,818
  • 1
  • 24
  • 18
  • it worked for me, too. Very strange why this error is here, and a potential source of confusion with a [much older error from 2016](https://sourceware.org/bugzilla/show_bug.cgi?id=202660). It can't possibly be the same error as that because 8.0.1 doesn't have this problem. I filed bug reports at [the homebrew page](https://github.com/Homebrew/homebrew-core/issues/25172) as well as [sourceware bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=22960) – xdavidliu Mar 13 '18 at 19:28
  • A better alternative is to install `gdb` from [Macports:](https://www.macports.org/) `sudo port install gdb`. Macports has better support for downgrading / upgrading and they are often more stable by not upgrading packages too soon (while still being up-to-date). Macports also generally feels faster and has support for `sudo`, among many other things. – GDP2 May 15 '18 at 17:59
  • 2
    After a bunch of experimentation, I think this is a bug that was introduced in commit f6ac5f3d63e03a81c4ff3749aba234961cc9090e. See the thread for this bug: https://sourceware.org/bugzilla/show_bug.cgi?id=22960 – saagarjha May 24 '18 at 08:15
  • Need a better QA/versioning/installation/ guide on stack exchange. – technazi Sep 08 '18 at 19:56
  • Many thanks, it works for me now (macOS 10.12.6). I think using gdb 8.0.1 is the best solution now, until they fixed the issue in 8.1.1 (current version). – Tien Do Sep 20 '18 at 06:02
  • 1
    Looks like this is still an issue in 8.2. – Shane Hughes Oct 05 '18 at 16:43
  • 1
    Shoot. Unfortunately this doesn’t seem to fix the problem any more on Mojave, and even with the newest “fix on mojave” commits to the gdb formula the error persists (even after code-signing gdb). – Konrad Rudolph Dec 13 '18 at 16:20
  • GDB 8.2 and Mojave -- still doesn't work. (sigh) I spent a couple hours trying to figure out what I'm doing wrong, before found this post. Thanks mate! – Artem Pianykh Dec 21 '18 at 15:48
  • 1
    This does not work on macOS 10.14.2, would be more useful to list what's the cause. – Michael Foukarakis Dec 22 '18 at 19:38
  • and... Is this related to [GDB/bin/bash can't execute the app in Eclipse CDT?](https://stackoverflow.com/questions/58318816/gdb-bin-bash-cant-execute-the-app-in-eclipse-cdt) – Danijel Oct 14 '19 at 06:49
3

in my case downgrading to 8.0.1 didn't help.
but the following steps helped.

(i inserted step 12, "check if it works" because, instead of restarting i've tested gdb and it worked just fine. i didn't follow the steps 12+)


  1. Open Keychain Access
  2. In menu, open Keychain Access > Certificate Assistant > Create a certificate
  3. Give it a name (e.g. gdb-cert)
    • Identity type: Self Signed Root
    • Certificate type: Code Signing
    • Check: let me override defaults
  4. Continue until "specify a location for..."
  5. Set Keychain location to System
  6. Create certificate and close Certificate Assistant.
  7. Find certificate in System keychain.
  8. Double click the certificate
  9. Expand Trust, set Code signing to always trust
  10. Restart taskgated in terminal: killall taskgated
  11. Codesign gdb using your certificate: codesign -fs gdb-cert /usr/local/bin/gdb
  12. --- CHECK IF IT WORKS ---
  13. Shut down your mac and restart in recovery mode (hold down command-Runtil apple logo appears)
  14. Open terminal window
  15. Modify System Integrity Protection to allow debugging: csrutil enable --without debug
  16. Reboot your Mac
  17. Debugging with gdb should now work as expected.

information source:
pre Sierra: https://gist.github.com/hlissner/898b7dfc0a3b63824a70e15cd0180154
Sierra: https://gist.github.com/gravitylow/fb595186ce6068537a6e9da6d8b5b96d

GnoStiC
  • 552
  • 4
  • 7
  • 7
    This is an unrelated issue, unfortunately. – saagarjha May 24 '18 at 08:16
  • 1
    These steps are summarized at the official Wiki: https://sourceware.org/gdb/wiki/PermissionsDarwin . I don't believe that the csrutil step is necessary anymore. – DomQ Jan 02 '19 at 16:15
  • Mac OS Sierra 10.13.6 gdb 8.0.1 I discovered that I already had gdbcert1 in my System. Followed everything from 7. Works! – Sergej Fomin May 31 '19 at 09:12
0

I'm using macOS 10.13.6 and I was having the same issue that " (please check gdb is codesigned - see taskgated(8))" ...

When I downgraded the GDB from 8.2.1 to 8.0.1 and then created and signed the gdb-cert again and it simply worked..

Don't forget to change the path of gdb from 8.2.1 to 8.0.1 in debugger of eclipse, restart and it will work.

AN0007
  • 21
  • 1