Gdb For Mac

Posted on  by

This page explains how to build and install gdb on OS X using #Homebrew or #Fink. Since OS X Mavericks 10.9, Xcode 5 no longer installs gdb by default and not globally. The officially supported debugging package for Mac OS X is lldb, which is a fine debugger. However, many people including me still prefer to use gdb, the GNU debugger. In fact, I use cgdb, which provides nice color interface to gdb, and that is why I must install gdb on my Mac. After installing gdb from homebrew (via $ brew install gdb), I followed these instructions to give gdb permissions to attach to a process. When I got to the step that runs the command: $ codesign -entitlements gdb-entitlement.xml -fs gdb-cert $(which gdb) I get the following error, with an exit code of 1: /usr/local/bin/gdb. How to install gdb (debugger) in Mac OSX El Capitan? Ask Question Asked 3 years, 10 months ago. Active 10 months ago. Viewed 103k times 40. How to install gdb (debugger) in Mac OSX El Capitan? I have tried installing gdb but failed couple of time. GDB: The GNU Project Debugger GDB Maintainers contributing current git documentation mailing lists Download GDB The most recent.

*** 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:

Mac Gdb 8.0.1

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>
For <string>/usr/libexec/taskgated</string>
<string>-sp</string>
</array>
5. (Optional) Re-enable SIP by repeating steps 1~3 with the command and reboot.

6. Add your username to procmod group
$ sudo dseditgroup -o edit -a $USER -t user procmod
$ 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!

Installing G++ on a Mac

This section is intended to get you quickly started with C++ programming on your Mac. We'll be installing GCC 4.8.1 and GDB through a tool called Homebrew. If you want an additional guide on all of the following steps (except for installing GCC), the one by Moncef Belyamani is quite helpful. When you follow it, ignore anything about installing Ruby; that is, stop after setting up git.

Homebrew

Homebrew 'installs the stuff that you need that Apple don't'. It's like Ubuntu's apt-get, where one can install packages easily from repositories. Instead of having to download, configure, and install something yourself, all you need to do is run one command, and Homebrew will take care of the rest for you.

Pre-requisites

Homebrew requires that you have either Xcode or the Xcode command line tools installed on your Mac. Xcode is a free integrated development environment similar to Eclipse designed by Apple and mainly intended for iOS development or targeting the clang compiler. In this class, we will focus on gcc.

Xcode is quite a big install, so if you do not want to install it, you can get away with just installing the Xcode command line tools. See a Stackoverflow discussion for instructions on how to install the command line tools regardless of whether you have Xcode installed.

Installing Homebrew

You need xcode command line tools to install Homebrew. It is very easy to install Homebrew. Open your terminal, and run the following command:

If this doesn't seem to do anything, try killing it (CTRL C) and running it again. Or checkout homebrew website.

GCC and GDB

Installing GCC

As mentioned before, installing packages with Homebrew is very easy. First, we will add the repository from which the GCC package is available, so that Homebrew knows where to find the package we want. The repository is at https://github.com/Homebrew/homebrew-versions.

We do this by using the brew-tap command. Keep your terminal open, and run the following command. (For more information on how brew-tap works, visit the Homebrew docs):

2.Specify the partition scheme, file system, cluster size and format options accordingly. No extra ISO file is required as this program will provide one by default. 4.Click Start button.There you have learnt how to create bootable USB from ISO with Rufus, and what you do with the UFD is really up to you.If you don’t want to download and install Windows to your Mac while need to burn a bootable password reset disk to save your locked Windows PC, you can try the Rufus alternative for Mac OS computer –. 3.Browser and choose an ISO image stored on your Mac. Rufus for macx.

Next, we will actually install the GCC package. Run the following command:

How to install gdb

It might take a while before the installation is complete. When done, run the following:

The result should look like this:

USC Wireless Warning

Many people have had issues running the brew install commands while connected to USC Wireless. If you are having trouble, you can either try using a wired connection, a different wireless connection, or do the following:

  1. Download a homebrew cache
  2. Open Finder, press CMD (command) + SHIFT + G and type /Library/Caches/Homebrew
  3. Extract the contents of the .zip you downloaded inside of the folder you opened in the previous step. Do not extract any of the .tar.bz2 or .tar.gz inside of the .zip folder.This should look as follows:

  4. Run brew install gcc48 in the Terminal as instructed above.

Using G++

To compile with the newly installed G++ compiler, use g++-4.8.

(Advanced) Aliasing g++

If you prefer calling g++ directly, you can also create a bash alias, as follows:

Put these two lines at the end of the file ~/.bashrc, and run:source ~/.bashrc

For more information on bash alias, take a look at the GNU Docs.

Installing GDB

Here also we use Homebrew. The following instruction has been taken from GDB on OS X Mavericks and Xcode 5 guide. To install, run the following brew command.

Check if it's installed:

The result should be gdb version 7 or higher.

Install Gdb For Mac

Codesigning gdb

Gdb For Mac Sierra

gdb is not going to debug yet. You'll get an error message like 'please check gdb is codesigned'. You need to create a certificate and sign gdb. By doing so you're telling the operating system that gdb is authorized to attach to other processes for debugging purposes. The following instructions have been taken from this Code Signing guide.

Gdb Macos Mojave

  1. Open application 'Keychain Access' (/Applications/Utilities/Keychain Access.app)
  2. In Keychain Access, select the 'login' keychain in the 'Keychains' list in the upper left hand corner of the window.

  3. Open the menu item in /Keychain Access/Certificate Assistant/Create a Certificate..

  4. Choose a name ('lldb_codesign' in the example, but you can use anything you want), set 'Identity Type' to 'Self Signed Root', and set 'Certificate Type' to 'Code Signing'. Click 'Create'.

  5. Click continue, continue and done.

  6. Click on the “My Certificates” category on the left side and double click on the new “lldb_codesign” certificate.

  7. Open the context menu for 'Trust' (click the triangle) and change the following:When using this certificate: Always Trust

  8. Now close this window, and enter your login password to confirm this change.

  9. Option-drag (this meaning holding the option key down and dragging) the new 'lldb_codesign' certificate from the login keychain to the System keychain in the Keychains pane of the main Keychain Access window to make a copy of this certificate in the System keychain. You'll have to authorize a few more times, set it to be 'Always trusted' when asked.

  10. Switch to the 'System' keychain and drag a copy of the 'lldb_codesign' you just made onto the Desktop.

  11. Switch to Terminal and then run the following command (copy paste it!): sudo security add-trust -d -r trustRoot -p basic -p codeSign -k /Library/Keychains/System.keychain ~/Desktop/lldb_codesign.cer

  12. Then right click on the 'lldb_codesign' certificate in the 'System' keychain (not 'Login') and select 'delete' to delete it from the 'System' keychain.

  13. Then reboot your system/computer.

  14. Finally you can sign gdb:codesign -s lldb_codesign /usr/local/bin/gdb

  15. If this command doesn't work..then panic! Just kidding, be sure that you have gdb installed and that gdb is actually installed in /usr/local/bin. You may want to try 'which gdb' in your Terminal to figure out where it is.

  16. Finally, remove the lldb_codesign.cer file that's sitting on your desktop, and gdb should be working at this point. :)