Debugging/Single Stepping GCC (GNU C Compiler) with Eclipse

Debugging/Single Stepping GCC (GNU C Compiler) with Eclipse

I am doing the below steps in a Lubuntu running on VirtualBox. I usually do messy things on a virtual machine as it does not disturb my actual OS.

The GCC source code can be downloaded from the official page : http://ftpmirror.gnu.org/gcc/gcc-5.3.0/gcc-5.3.0.tar.bz2

Change directory to where the sources were downloaded. Extract the sources by running the following command in the terminal:

tar jxf gcc-5.3.0.tar.bz2

Install the prerequisites that are required for the gcc build.

cd gcc-5.3.0
./contrib/download_prerequisites

Some other prerequisites might be required to be installed.

sudo apt-get install build-essential gawk m4 gcc-multilib

Create an “objdir” outside the extracted GCC sources and change directory to it.

cd ..
mkdir objdir
cd objdir

Run the following commands to build the GCC (prefix is set so that it does not affect the already installed gcc version) :

$PWD/../gcc-5.3.0/configure --prefix=$HOME/gcc-5.3.0 --enable-languages=c --disable-multilib
make STAGE1_CXXFLAGS="-g -O0" all-stage1
sudo make install

If required install Eclipse with CDT with the following command:

sudo apt-get install eclipse eclipse-cdt eclipse-jdt

Create a new Eclipse project with “Makefile project with existing code”.

MakeFileWithExistingCode

Import the sources :

import

Open the “Debug” perspective:

Perspective

Create a “hello.c” file in your home folder which has to be compiled by the GCC which we have built.
/* Hello World program */
#include <stdio.h>
main()
{
printf(“Hello World”);
}

Create a .gdbinit file in the home folder with the following contents:

set follow-fork-mode child

Create the debug configuration as follows.

DebugConfig_1DebugConfig_2

Select the options related to “Non-Stop mode”, Force thread list..” and “Automatically debug forked…”DebugConfig_3

Begin the debug session by clicking on the bug symbol at the top. The execution stops at the main function of the gcc driver.

debug_1

Put a breakpoint in the actual c compiler which the gcc driver calls. The file is at gcc-5.3.0/gcc/main.c.

debug_2

Run the code further and it will hit the breakpoint that was set in the c compiler.  Here you are debugging/single stepping the gnu c compiler.

Leave a Reply

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