Debugging
Usually, a compiled executable does not have references of either
source-code variable names or line numbers. However, an executable
compiled for debugging retains these references. This allows:
- program tracing - suspend the execution of the program when
it reaches a specified line (called breakpoint) in the source
file or even suspend it after every line. Thus allowing line by
line execution.
- variable lookup - observe the values of the variables.
Debugging in GCC is done using a command line debugger
called gdb. CDT of Eclipse provides a graphics front end
to gdb. By default, CDT compiles all programs ready for debugging.
To debug a built program, press F11 or click on debug
icon on the CDT toolbar. This opens a debug perspective. Debug
perspective has several major views.
- Variables view shows the
values of the variables.
- Editor view shows the source program
with the line where the execution is suspended highlighted.
- Debug view shows the status of debugging.
By default. The debugged program automatically suspends in the first
statement of the main function.
Hitting key F6 or clocking on the appropriate icon on
the toolbar in debug view, advances the program execution exactly one
line. Repeatedly "stepping over" the lines of the program in this
fashion allows the programmer to trace the program execution in detail.
Note that cygwin does not interact with standard Eclipse
console well. Therefore, for debugging, CDT launches a separate window
for input and output.
Note also that cygwin debugger could not find source files
by default. Which means that when you run debugger for the first time,
the debugger may not show the source code. To fix that, follow the directions
here
Breakpoints
Breakpoint is a line in the program where the execution
stops. Tracing the program step-by-step may become tedious, especially
if the program is large. Instead a breakpoint may be set at the
particular line of interest.
To add a breakpoint in Eclipse, double click the marker bar located on
the left margin of the editor view beside the line of code where you
want to add a breakpoint. A blue dot is displayed in the marker bar
and in the Breakpoints view, along with the name of the associated
file. To clear (remove) a breakpoint, double-click on the blue dot
associated with it.
Displaying Arrays in Functions
After array is passed to a function as a parameter, its size
information is lost. The debugger cannot present the array contents
properly. To display the array information, right-click on the array
name and choose: "Display As Array" option. A dialog box appears
prompting you to enter the first index and the array
size. Henceforward, Eclipse will display this array with the size
specified. To change the size of this array or undo this modification
right-click on the array name and select "Restore Original Type".