How to setup gdb and Eclipse to debug C++ files on macOS Sierra

Using gdb debugger on macOS is no longer easy since Xcode stopped using it and replaced it with lldb. On macOS Sierra there are several steps to follow in order to make it work.

Install gdb

The easier way to install gdb is by using Homebrew: “the missing package manager for macOS”. If you don’t have it installed, open your Terminal prompt and write this command:

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

Once you have Homebrew installed, you can simply run:

brew install gdb

Now check gdb installation by running:

gdb --version

Take note of the version: you’ll need it later.

Generate a certificate

Installing gdb is not enough. If you try debugging a file, you’ll get an error since the Darwin kernel doesn’t allow gdb to control another process without having special rights. For security reasons this is the default behavior.

In order to give gdb those special rights, you need to generate a certificate.

  1. Launch Keychain Access application: Applications > Utilities > Keychain Access
  2. Go to Keychain Access > Certificate Assistant > Create a Certificate…
  3. Choose a name (e.g. gdb)
  4. Set Identity Type to Self Signed Root
  5. Set Certificate Type to Code Sign
  6. Check the Let me override defaults checkbox

At this point you can go on with the installation process until you get the Location for the certificate dialog box. Here you need to set Keychain to System. Finally you can click on Create button.

After these steps, you can see the new certificate under System keychains. From the contextual menu of the new certificate (right-click on it) select the Get info option. In the dialog box expand the Trust item and set Code signing to Always Trust.

Finally, reboot your system.

Sign the certificate for gdb

It’s time to sign the certificate. Open your Terminal prompt and run:

codesign -s gdb-cert gdbPath

where gdbPath is the full path to your gdb binary file. If you have installed gdb as explained before (using Homebrew), the path should be: /usr/local/Cellar/gdb/version/bin/gdb (replace version with the actual version of your gdb installation, e.g. 7.12.1).

Create a gdb command file

This step is specifically needed for macOS Sierra (in previous versions it shouldn’t be necessary).

In the home directory create a new file called .gdbinit and write the following command in it: set startup-with-shell off.

Now you can use gdb for debugging files on your Mac. If you use Eclipse, follow the next step.

Set Eclipse for using gdb

If you want to use gdb inside Eclipse, you need to set some options.

  1. Go to Eclipse > Preferences
  2. From the left menu select C/C++ > Debug > GDB
  3. Set GDB debugger to the full path of your gdb binary file (the same used for signing the certificate)
  4. Set GDB command file to the full path of your .gdbinit file (it should be /Users/yourname/.gdbinit, where yourname is your username).
  5. Click on Apply button.

Now, you can debug files from inside Eclipse using gdb.

Resources

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *