GDB Installation on Mac OS

Step 1. Install gdb

  1. To install gdb by the following command on terminal:

    1
    $ brew install gdb
  2. We can confirm whether gdb is installed successfully by running:

    1
    $ gdb --version

    On my Mac, we can see:

    gdbversion
  3. However, we will get the information as follows while try to run a program:

    1
    2
    Unable to find Mach task port for process-id XXXXX: (os/kern) failure (0x5).
    (please check gdb is codesigned - see taskgated(8))

Step 2. Create a Certificate

  1. Open Launchpad -> Other -> Keychain Access

  2. In its menu, open Keychain Access -> Certificate Assistant -> Create a certificate

  3. While creating, we set Identity type to Self Signed Root, Certificate type to Code Signing, and check “let me override defaults”.

    creategdbcert
  4. We just continue until we get a page which prompts us for “specify a location”, and select Keychain location to System. Then, create a certificate and close assistant.

    Noting: If we get an error as follows, we need to repeat the whole Step 2, but set Keychain location to Login in 4.

    1
    Unknown Error = -2,147,414,007
  5. Find the certificate we just created, and double click it. Then, modify Trust, set Code Signing to Always Trust.

    gdbtrust
  6. Restart taskgated in terminal by running:

    1
    $ sudo killall taskgated
  7. Create a file called gdb-entitlement.xml which allows OS to trust gdb for debugging:

    1
    $ vim gdb-entitlement.xml

    which includes:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>com.apple.security.cs.allow-jit</key>
    <true/>
    <key>com.apple.security.cs.allow-unsigned-executable-memory</key>
    <true/>
    <key>com.apple.security.cs.allow-dyld-environment-variables</key>
    <true/>
    <key>com.apple.security.cs.disable-library-validation</key>
    <true/>
    <key>com.apple.security.cs.disable-executable-page-protection</key>
    <true/>
    <key>com.apple.security.cs.debugger</key>
    <true/>
    <key>com.apple.security.get-task-allow</key>
    <true/>
    </dict>
    </plist>
    </pre>
  8. Run codesign on terminal:

    1
    $ codesign --entitlements gdb-entitlement.xml -fs gdb-cert /usr/local/bin/gdb

    And, we will ask for inputting the root password.

  9. Finally, reboot!

Step 3. Runing gdb

  1. Create HelloWorld.c using C:

    1
    2
    3
    4
    5
    6
    #include <stdio.h>

    int main(){
    printf("Hello World!\n");
    return 0;
    }
  2. Compile it with -g option:

    1
    $ gcc HelloWorld.c -g -o HelloWorld
  3. Run gdb to debug this program. Successfully!!!

    gdbdebug

    Noting, if we find the following information, and the debugger is stopped:

    1
    [New Thread 0x1a03 of process 3822]

    We need to type the following into the terminal, and restart a debugger:

    1
    echo "set startup-with-shell off" >> ~/.gdbinit