Configure GDB for CLion on MacOS to debug Centos in Vagrant box
Tough title. Basically, I’ll share here my experience in setting up GDB to work with CLion on a MacOS (HighSierra) to debug C programs running on a Centos in a Vagrant (Virtualbox) machine.
The first step, download GDB source code on your MacOS and compile it like this (from inside the source code folder):
./configure --target=x86_64-linux-gnu
make
make install
You might need to copy the compiled file to the bin folder
cp ./gdb/gdb /usr/local/bin/
As a second step, we configure the CLion.
- Click on + and select GDB Remote Debugging
- Fill in a profile name (Eg: GDB)
- Select the GDB executable file from where you put it. If you followed the previous instructions, that should be /usr/local/bin/gdb
- Set the debug target system. In my case, the domain I use to access my Vagrant box is c1.dev and the port which I use (set later) is 2000. So, the value I use here is tcp:c1.dev:2000
- Select (type) the symbol file. As the compiled file contains the debug symbols, just type here the full path to your compiled file. In my case, this is /Users/razvan/Projects/c1/factorial
- Set the path mapping according to your Vagrant shared folder settings. Most of us use the /vagrant folder inside the box. So, for the remote folder, I’ve set /vagrant and for the local folder the folder where my file resides.
- Apply changes
- And save
That should be enough for configuration. Now let’s move to the remote (Vagrant) environment. Here we need to install the gdbserver. To do this just run sudo yum install -y gdb-gdbserver
Inside the box, compile your C file with debugging symbols:
gcc -ggdb factorial.c -o factorial
The output file (-o) must match the file you specified for the symbols in the CLion configuration.
Then let’s start the gdbserver like this:
gdbserver localhost:2000 factorial
You guessed, here we specify the port number to the same value set for CLion configuration.
Now set your breakpoints and run debug from CLion.
You’ll have to restart the gdbserver each time your process exits.