Sunday, April 24, 2016

How to Enable GDB on Mac OS X El Capitan (NOT RECOMMENDED)

*** I do not recommend the instructions below ***
*** Please take a look at this post for safer method ***

I have a macbook air (MBA) which I carry around to use at various locations other than my desk at home. Of course I could install Ubuntu on my MBA, but I really like how easy it is to do anything on OS X,  so I am keeping it. I also do a lot of software development on my MBA. That being said, it bothers me that by default I cannot run gdb on OS X El Capitan. In this post, I will show how to enable gdb on OS X El Capitan. The credit goes to here.

First, you will need to install gdb. I would use brew. In case you don't have brew installed on the system, follow the instructions here.
$ brew install gdb

When you try to run a program on gdb, you will encounter error similar to below:
(gdb) run 
Starting program: a.out
Unable to find Mach task port for process-id 627: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))

The solution is as follows:
1. Restart OS X. Enter recovery mode by pressing and holding [command + R] until you see Apple logo. See here for more detail.

2. In the recovery mode, choose utilities menu and open up terminal

3. In the terminal, disable system integrity protection (SIP)
$ csrutil disable && reboot

4. Add -p option to /System/Library/LaunchDaemons/com.apple.taskgated.plist file. After your edit, it should read something like (line 22)
<array>
    <string>/usr/libexec/taskgated</string>
    <string>-sp</string>
</array>

5. (Optional) Re-enable SIP by repeating steps 1~3 with the command and reboot.
$ csrutil enable && reboot

6. Add your username to procmod group
$ sudo dseditgroup -o edit -a $USER -t user procmod

7. Locate gdb executable file and run
$ sudo chgrp procmod $(which gdb)
$ sudo chmod g+s $(which gdb)

Please be advised that you will need to reboot your system for the change to take effect.

Now, you should be able to use gdb on Mac OS X!


9 comments:

  1. Great! I was missing the steps #6 and #7 and now it works great.

    ReplyDelete
  2. at step 4 Im getting an error "The authenticated save for this file failed (application code: 20004)". any way around this?

    ReplyDelete
  3. It works! Appreciate your tutorial!

    ReplyDelete
  4. Hi! I'm trying this out on Sierra. Till step 6 I followed you fine, but I don't know what you mean by "locate gdb executable" in step 7. I'm sort of a newb at this. Can you elaborate on what exactly I have to enter in place of (which gdb)? TIA

    ReplyDelete
    Replies
    1. Type exactly as written, except the first dollar sign. $(whic gdb) is a part of the command to type in

      Delete