GDB Installation on Mac OS
Step 1. Install gdb
To install gdb by the following command on terminal:
1
brew install gdb
We can confirm whether gdb is installed successfully by running:
1
gdb --version
On my Mac, we can see:
However, we will get the information as follows while try to run a program:
1
2Unable 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
Open Launchpad -> Other -> Keychain Access
In its menu, open Keychain Access -> Certificate Assistant -> Create a certificate
While creating, we set Identity type to Self Signed Root, Certificate type to Code Signing, and check “let me override defaults”.
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
Find the certificate we just created, and double click it. Then, modify Trust, set Code Signing to Always Trust.
Restart taskgated in terminal by running:
1
sudo killall taskgated
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
<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>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.
Finally, reboot!
Step 3. Runing gdb
Create HelloWorld.c using C:
1
2
3
4
5
6
int main(){
printf("Hello World!\n");
return 0;
}Compile it with -g option:
1
gcc HelloWorld.c -g -o HelloWorld
Run gdb to debug this program. Successfully!!!
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