Skip to content

Instantly share code, notes, and snippets.

@danisfermi
Created December 15, 2017 23:00
Show Gist options
  • Save danisfermi/17d6c0078a2fd4c6ee818c954d2de13c to your computer and use it in GitHub Desktop.
Save danisfermi/17d6c0078a2fd4c6ee818c954d2de13c to your computer and use it in GitHub Desktop.
Setup gdb on Mac OS Sierra/High Sierra

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:

  1. Open Keychain Access
  2. In menu, open Keychain Access > Certificate Assistant > Create a Certificate
  3. Give it a name (e.g. gdbcert)
  • Identity type: Self Signed Root
  • Certificate type: Code Signing
  • Check: Let Me Override Defaults
  1. Continue until "Specify a Location For"
  2. 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.
  3. Create certificate and close Certificate Assistant.
  4. Find the certificate in System keychain.
  5. Double click certificate.
  6. Expand Trust, set Code signing to Always Trust
  7. Restart taskgated in terminal: killall taskgated
  8. 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.
  9. Codesign gdb using your certificate: codesign -fs gdbc /usr/local/bin/gdb
  10. Shut down your mac and restart in recovery mode (hold down command-R until apple logo appears)
  11. Open terminal window
  12. Modify System Integrity Protection to allow debugging: csrutil enable --without debug
  13. Reboot your Mac. Debugging with gdb should now work as expected.
@SyntevoAlex
Copy link

SyntevoAlex commented Jun 17, 2019

Stacks are shown wrong for system libraries
Workaround: run debugged process with environment DYLD_SHARED_REGION=avoid.

Explanation:
This is due to dyld cache. When system libraries are loaded from cache, their segments are split in groups: first all __TEXT segments, then all __DATA segments, etc (this can be seen in vmmap --interleaved). However, GDB expects segments for a single library to follow each other (see GDB's code darwin_relocate_section_addresses). Apple's fork of GDB had support for dyld cache, see get_sectoffs_for_shared_cache_dylib, but Apple's GDB was discontinued in 2013 and replaced with LLDB. So the simplest workaround is to disable dyld cache for specific process.

@rustymagnet3000
Copy link

@abetancort
Copy link

@rustymagnet3000 Thanks for the heads up. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment