41

How to install gdb (debugger) in Mac OSX El Capitan ? I have tried installing gdb but failed couple of time.

I was following this URL : http://ntraft.com/installing-gdb-on-os-x-mavericks/ , MAC doesnot allow to install MacPorts.

Could anyone please help me in this regard.

Tauheed Khan Mohd
  • 513
  • 1
  • 4
  • 6

8 Answers8

30

Install Homebrew first :

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then run this : brew install gdb

Sankalp Singha
  • 4,461
  • 5
  • 39
  • 58
28

Please note that this answer was written for Mac OS El Capitan. For newer versions, beware that it may no longer apply. In particular, the legacy option is quite possibly deprecated.

There are two solutions to the problem, and they are both mentioned in other answers to this question and to How to get gdb to work using macports under OSX 10.11 El Capitan?, but to clear up some confusion here is my summary (as an answer since it got a bit long for a comment):

Which alternative is more secure I guess boils down to the choice between 1) trusting self-signed certificates and 2) giving users more privileges.

Alternative 1: signing the binary

If the signature alternative is used, disabling SIP to add the -p option to taskgated is not required.

However, note that with this alternative, debugging is only allowed for users in the _developer group.

Using codesign to sign using a cert named gdb-cert:

codesign -s gdb-cert /opt/local/bin/ggdb

(using the MacPorts standard path, adopt as necessary)

For detailed code-signing recipes (incl cert creation), see : https://gcc.gnu.org/onlinedocs/gcc-4.8.1/gnat_ugn_unw/Codesigning-the-Debugger.html or https://sourceware.org/gdb/wiki/BuildingOnDarwin

Note that you need to restart the keychain application and the taskgated service during and after the process (the easiest way is to reboot).

Alternative 2: use the legacy option for taskgated

As per the answer by @user14241, disabling SIP and adding the -p option to taskgated is an option. Note that if using this option, signing the binary is not needed, and it also bypasses the dialog for authenticating as a member of the Developer Tools group (_developer).

After adding the -p option (allow groups procmod and procview) to taskgated you also need to add the users that should be allowed to use gdb to the procmod group.

The recipe is:

  1. restart in recovery mode, open a terminal and run csrutil disable

  2. restart machine and edit /System/Library/LaunchDaemons/com.apple.taskgated.plist, adding the -p opion:

    <array>
        <string>/usr/libexec/taskgated</string>
        <string>-sp</string>
    </array>
    
  3. restart in recovery mode to reenable SIP (csrutil enable)

  4. restart machine and add user USERNAME to the group procmod:

    sudo dseditgroup -o edit -a USERNAME -t user procmod

    An alternative that does not involve adding users to groups is to make the executable setgid procmod, as that also makes procmod the effective group id of any user executing the setgid binary (suggested in https://apple.stackexchange.com/a/112132)

    sudo chgrp procmod /path/to/gdb
    sudo chmod g+s /path/to/gdb 
    
drRobertz
  • 3,490
  • 1
  • 12
  • 23
  • 2
    I want to chime in here and say that after some 4-5 hours of trying to wade through the countless variations of Alternative 1 (key-signing) and not having it work (I do not seem to be alone in this), I finally went with Alternative 2 which worked like a charm. Notice that indeed you have to disable the SIP in OSX El Capitan. Thank god! This issue was driving me absolutely insane. – TSGM Feb 05 '16 at 03:26
  • @TSGM just out of curiosity, is your user member of the _developer group? That was the part I was missing since the group membership etc. now is so wonderfully obfuscated (compared to normal *nix) with Apple's dseditgroup. – drRobertz Feb 05 '16 at 18:55
  • @Marinos K, Could you elaborate on how it failed? Is your user member of _developer? – drRobertz Feb 23 '16 at 09:11
  • 2
    yes it is (and by the way i can't seem to remove it now.. (see http://stackoverflow.com/questions/35573402/remove-user-from-group-mac-os-x-el-capitan ). Well it simply failed, I did everything mentioned, I verified that the binary is correctly signed and I still got the same error. The other option did the trick though. – Marinos K Feb 23 '16 at 09:20
  • OK if the legacy option works for you that's fine, (I also prefer that option as it is more *nix-y) I'm just a bit worried that it is not very future-proof. About alternative 1, you don't write what "the same error is", but what's expected to happen is that the first time in a session you run your debugger, you should get a popup requesting you to authorize as a member of the _developer group before the debugger is launched. – drRobertz Feb 23 '16 at 09:50
  • @SRobertz a. the error is that one: `Unable to find Mach task port for process-id 576: (os/kern) failure (0x5). (please check gdb is codesigned - see taskgated(8))`. b. the situation is more complex eventually - method 2 did work as far as gdb is concerned BUT seriously messed up my system so that no third party (excluding built-in mac os X apps) would launch any more (all reported an exception relevant to code signature) - I found no way to revert other than using time-machine and after that I double checked that it was method 2 that caused the problem - all these with a MacBook pro 10.11.3 – Marinos K Feb 23 '16 at 22:13
  • Thanks again. I saw option two and I thought "screw that" because it looked messy, but after hours trying variants of option 1, I gave up and tried option 2 and it worked straight away! Would absolutely recommend running a timemachine backup first just in case. – Derwent Mar 07 '16 at 04:48
  • method 1 failing for me so far. `id` has a `204(_developer)` entry, and when i retry `codesign` it tells me `gdb` is already codesigned. `taskgated` has been restarted (new pid). unsure what else i could recheck. going to try method 2 now. thanks for documenting your process. – Alex Moore-Niemi Mar 10 '16 at 03:24
  • alternative 2 + alternative method at bottom of it worked for me. (make the executable setgid procmod,) – Alex Moore-Niemi Mar 10 '16 at 03:45
  • A related issue is that gdb uses the shell to launch the debugged process, by default. System Integrity Protection prevents some processes, including the shell, from inheriting LD_LIBRARY_PATH or DYLD_ environment variables from their parents, meaning that gdb will be unable to pass those to the debugged programs unless you stop it from launching the inferior process via the shell. If you need to pass those vars to a debugged program with SIP enabled, you can prevent gdb from launching via the shell by doing "set startup-with-shell off" from the gdb prompt. – 1203_dube Dec 18 '16 at 21:06
  • 1
    The (Sierra) taskgated man page notes “procmod and procview support (-p) was removed in 10.11”. So alternative 2 is probably no longer on the table. – John Marshall Apr 28 '17 at 09:29
  • To install gdb `brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/master/Formula/gdb.rb` – zed Aug 14 '17 at 14:09
13

This doesn't necessarily address the question but if you are using Mac OS X then you can probably use lldb LLDB Homepage . It's very similar to gdb and even provides a guide to using commands that you would use on gdb.

13

Here's a blog post explains it very well:

http://panks.me/posts/2013/11/install-gdb-on-os-x-mavericks-from-source/

And the way I get it working:

  1. Create a coding signing certificate via KeyChain Access:

    1.1 From the Menu, select KeyChain Access > Certificate Assistant > Create a Certificate...

    1.2 Follow the wizard to create a certificate and let's name it gdb-cert, the Identity Type is Self Signed Root, and the Certificate Type is Code Signing and select the Let me override defaults.

    1.3 Click several times on Continue until you get to the Specify a Location For The Certificate screen, then set Keychain to System.

  2. Install gdb via Homebrew: brew install gdb

  3. Restart taskgated: sudo killall taskgated && exit

  4. Reopen a Terminal window and type sudo codesign -vfs gdb-cert /usr/local/bin/gdb

ryenus
  • 15,711
  • 5
  • 56
  • 63
10

On my Mac OS X El Capitan, I use homebrew to install gdb:

brew install gdb

Then I follow the instruction here: https://sourceware.org/gdb/wiki/BuildingOnDarwin, in the section 2.1. Method for Mac OS X 10.5 (Leopard) and later.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Châu Hồng Lĩnh
  • 1,986
  • 1
  • 20
  • 23
2

Once you get the macports version of gdb installed you will need to disable SIP in order to make the proper edits to /System/Library/LaunchDaemons/com.apple.taskgated.plist. To disable SIP, you need to restart in recovery mode and execute the following command:

csrutil disable

Then restart. Then you will need to edit the bottom part of com.apple.taskgated.plist like this:

<array>
        <string>/usr/libexec/taskgated</string>
        <string>-sp</string>
</array>

Then you will have to restart to have the changes take effect. Then you should reenable SIP. The gdb command for the macports install is actually ggdb. You will need to code sign ggdb following the instructions here:

https://gcc.gnu.org/onlinedocs/gcc-4.8.1/gnat_ugn_unw/Codesigning-the-Debugger.html

The only way I have been able to get the code signing to work is by running ggdb with sudo. Good luck!

user14241
  • 727
  • 1
  • 8
  • 27
  • with the -p option to taskgated, signing the binary is not required. But note that the users that should run gdb must be added to the `procmod` group. – drRobertz Dec 11 '15 at 11:23
1

Just spent a good few days trying to get this to work on High Sierra 10.13.1. The gdb 8.1 version from homebrew would not work no matter what I tried. Ended up installing gdb 8.0.1 via macports and this miraculously worked (after jumping through all of the other necessary hoops related to codesigning etc).

One additional issue is that in Eclipse you will get extraneous single quotes around all of your program arguments which can be worked around by providing the arguments inside .gdbinit instead.

llevar
  • 755
  • 8
  • 24
0

It seems that MacPorts could be installed in El Capitan right now: https://www.macports.org/install.php Then you probably can install gdb by link you mentioned.

m.aibin
  • 3,528
  • 4
  • 28
  • 47
  • 3
    One can install gdb via MacPorts onto El Capitan, but upon activation there is a warning that "You will need to make sure /System/Library/LaunchDaemons/com.apple.taskgated.plist has the '-p' option" and with El Capitan, files in /System are read-only even by root as allueded here: https://stackoverflow.com/questions/32665409/mac-os-x-cannot-run-gdb – j-beda Nov 11 '15 at 01:40