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

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

Install gdb

The easiest 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"

Once you have Homebrew installed, you can run:

brew install gdb

Now check gdb installation by running:

gdb --version

Take note of the version: you’ll need it later. In my case, it is 8.0.1.

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 behaviour.

To give gdb those permissions, you need to generate a certificate.

  1. Launch Keychain Access application: Applications > Utilities > Keychain Access.
  2. From the Keychains list on the left, right-click on the System item and select Unlock Keychain “System”.
  3. Go to Keychain Access > Certificate Assistant > Create a Certificate.
  4. Choose a name (e.g. gdb-cert).
  5. Set Identity Type to Self Signed Root.
  6. Set Certificate Type to Code Signing.
  7. Check the Let me override defaults checkbox.

At this point, you can go on with the installation process until you get the Specify a Location For The Certificate dialogue 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 newly created certificate (right-click on it) select the Get info option. In the dialogue box expand the Trust item and set Code signing to Always Trust.

Then, from the Keychains list on the left, right-click on the System item and select Lock Keychain “System”.

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 gdb-cert is the name of your certificate and 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. 8.0.1).

Create a gdb command file

You need this step if you’re on 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.

set startup-with-shell off
view raw .gdbinit hosted with ❤ by GitHub

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 (Oxygen, Neon), you need to set some options:

  1. Go to Run > Debug Configurations…
  2. Select a launch configuration from the list on the left (e.g. C/C++ Application)
  3. Open the Debugger tab from the menu on the right
  4. Set GDB debugger to the full path of your gdb binary file (the same used for signing the certificate)
  5. Set GDB command file to the full path of your .gdbinit file (it should be /Users/yourname/.gdbinit, where yourname is your username)
  6. Click on the Apply button.

Eclipse window for debug configurations

In case you are using an older version of Eclipse (Mars or previous), these are the steps to follow:

  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 the Apply button.

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


15 Replies to “How to setup gdb and Eclipse to debug C++ files on macOS Sierra”

  1. Do you have similar instructions for Mac OS 10.12.6? After I followed all your instructions here, gdb works okay on command line. However, when I run from eclipse, gdb complains that the work folder does not exist.
    : : No such file or directory.
    .gdbinit: No such file or directory.

    1. Hi, thanks for asking. I checked the process described in this article on macOS 10.12.6 and using both the two latest versions of Eclipse (Oxygen 1 and Neon 3). The sequence of steps to access the Eclipse debug configurations are slightly different (I updated this tutorial accordingly), but none that could have caused your problem.

      Have you verified that the .gdbinit file is actually in the path you set inside the Eclipse configurations? To do that, open a Terminal prompt, go to the home directory with

      cd ~

      (if you created the file inside this directory) and run the command

      ls -a .gdbinit

      If you get a result, the file does exist, otherwise you may have created it inside another directory.

      After this check, let me know if you still have problems with Eclipse.

  2. Make sure you UNLOCK system keychain before starting certificate assistant – this solved the problem I was getting wrt error code

    1. Good point, thanks for sharing! I updated the tutorial with a further step to unlock the System keychain before launching the Certificate Assistant and a step to lock it again after completing the process. Thank you for pointing that out!

  3. Great instructions. Thanks. I’ve just one issue. When trying to create the .gdbinit file, I got the following error message.

    You can’t use a name that begins with a dot “.”, because these names are reserved for the system. Please choose another name.

    I am on MacOs His Sierra (10.13.2). Can you tell me how to work around?

    1. Hello,

      you can create a file with a name starting with a dot in different ways.

      From the GUI, you can use names starting with a dot only if hidden files are shown. To show/hide hidden files you can use the shortcut cmd + shift + .

      From the Terminal, you can use whichever text editor (e.g. nano) you want without any restrictions on visibility.

  4. Hi , Thomas.

    Thanks for putting these instructions. I followed them , but still can’t run debug on Eclipse Oxygen.2. It says “Error in final launch sequence” and when I open Details I get :

    “Error in final launch sequence
    Failed to execute MI command:
    Error message from debugger back end:
    Unable to find Mach task port for process-id 651: (os/kern) failure (0x5).\n (please check gdb is codesigned – see taskgated(8))”

    I verified that gdb is codesigned :

    $ codesign -s gdb-cert /usr/local/Cellar/gdb/8.0.1/bin/gdb
    /usr/local/Cellar/gdb/8.0.1/bin/gdb: is already signed

    And .gdbinit file exists in my home directory :
    $ ls -a .gdbinit
    $ cat .gdbinit
    set startup-with-shell off

    What else could go wrong ?


  5. Thanks – there are many solutions out there, this is the one which worked for me. Important to set the .gdbinit path.

Leave a Reply

Your email address will not be published. Required fields are marked *