Here are the steps to installing and setting up GDB on Mac OS Sierra/High Sierra.
Run brew install gdb
.
On starting gdb, you will get the following error:
Unable to find Mach task port for process-id 2133: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))
To fix this error, follow the following steps:
- Open Keychain Access
- In menu, open Keychain Access > Certificate Assistant > Create a Certificate
- Give it a name (e.g.
gdbcert
)
- Identity type: Self Signed Root
- Certificate type: Code Signing
- Check: Let Me Override Defaults
- Continue until "Specify a Location For"
- Set Keychain location to System. If this yields the following error:
Certificate Error: Unknown Error =-2,147,414,007
Set Location to Login, Unlock System by click on the lock at the top left corner and drag and drop the certificate gdbcert to the System Keychain. - Create certificate and close Certificate Assistant.
- Find the certificate in System keychain.
- Double click certificate.
- Expand Trust, set Code signing to
Always Trust
- Restart taskgated in terminal:
killall taskgated
- Enable root account by following the steps given below: Open System Preferences. Go to User & Groups > Unlock. Login Options > "Join" (next to Network Account Server). Click "Open Directory Utility". Go up to Edit > Enable Root User.
- Codesign gdb using your certificate:
codesign -fs gdbc /usr/local/bin/gdb
- Shut down your mac and restart in recovery mode (hold down command-R until apple logo appears)
- Open terminal window
- Modify System Integrity Protection to allow debugging:
csrutil enable --without debug
- Reboot your Mac. Debugging with gdb should now work as expected.
With GDB 8.2.1 (through Homebrew) and the steps above (without the Enable Root User) I still keep getting the code sign error in Mojave and GDB:
I also created a new certificate and tried again but no difference.
I then tried using these steps (using entitlements) https://stackoverflow.com/a/52994006
Now I no longer get the taskgated/codesigning error but the debugger just quits, similarly to benoitgoyet post above.
Probably the same bug in 8.2 that is still present, see https://stackoverflow.com/questions/49001329/gdb-doesnt-work-on-macos-high-sierra-10-13-3