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.
@danisfermi please add a step to downgrade gdb as mentioned here: https://gist.github.com/danisfermi/17d6c0078a2fd4c6ee818c954d2de13c#gistcomment-2750786
also, on OSX Mojave, it fails because of https://stackoverflow.com/questions/52529838/gdb-8-2-cant-recognized-executable-file-on-macos-mojave-10-14 ; I wrote a temporary brew formula that makes it work, see https://stackoverflow.com/a/53586598/1426932