Main Navigation

Secondary Navigation

Page Contents

Contents

Getting Started

Before we start to debug our first program a short information on possible errors and their causes. More information on errors, resp. corresponding signals by the OS are available via a browser or locally displaying the corresponding manual page with the command:

man 7 signal
In most cases you will see a Segmentation fault, corresponding to the signal SIGSEGV, that indicates an Invalid memory reference. Not that common are Floating point exceptions (SIGFPE) indicating a division by 0 or similar.

The sources used are available for download. To step through the sources the compiled binaries have to contain the so called symbol table and other debug information and the execution order of statements has to match the order in the code. Therefore sources have to be compiled with the debug flag -g and it is recommended to avoid any code optimization with flag -O0 (minus capital O zero).

This holds for all compilers but we will concentrate on gcc or g++, the open source compilers for C or C++ of the GNU project.

The first example will be available for debugging after the command:

gcc -g -O0 -o simple1 simple1.c
Running the executable simple1 requires an argument and we start it with
./simple1 100
which results in a
Segmentation fault
indicating a Invalid memory reference.

Debugging starts with using the Debugger gdb of the GNU project

gdb ./simple1
which stops waiting for input
(gdb)
We start our program by typing run - in our case augmented with the command line argument, that is
run 100
Again, the program ends with a segmentation fault but now we get the additional information that something odd happens in line 12:
12 squares[i]=i*1.0f;
The advantage of using a debugger is, that you may query further information like print variable. Printing variable i, we learn that at the moment of the fault i=0 and print squares[0] returns the "strange" value -2.43060218e-33 [1]. We end the debug session typing quit.

Browsing the source code we observe squares, which is declared in line 5 as pointer without allocating memory to it. Accessing the not allocated memory location for squares[0] causes the memory violation.

Ok. That was easy.


Footnotes:
  • [1] This value is random and may change from run to run, with compiler version, ...