Chapter P5. The debugging of C programs
| Goals for this chapter:
|
-
gdb (gdb)
-
make (make)
-
cc (cc)
-
strip (binutils)
-
touch (fileutils)
-
xxgdb (xxgd)
-
ddd (ddd)
|
Don't interrupt me while I'm interrupting.
Winston S. Churchill.
"Given enough eyeballs, all bugs are shallow."
Eric S. Raymond
What is a debugger ?
This is the chapter about debuggers. The debug or debugger is the name
that US programmers assign to a program, the debug capable to trace the
program execution step-by-step following the original source(s).
The word "debug" means no bugs, following the common name "bug", used
to understand the small misfunctions inside the code.
The "gdb" debugger
In Linux, an OpenSource GNU based Operating System, the normal debug is
the "gdb", GNU debugger. Of course because GNU supports C, C++, Fortran,
Java and also Ada, the debuggers must also support the debugging of these
languages.
To compile a program that we plan to debug we need to compile it with
the "-g" flag. Otherwise the symbolic information will not be included.
[root@ftosx1 Chap4]# gcc -g malloc.c -o malloc
Then, we can launch the debugger with the binary that we will debug,
as a parameter.
[root@ftosx1 Chap4]# gdb malloc
GNU gdb 4.18
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License,
and you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty"
for details.
This GDB was configured as "i386-COL-linux"...
(gdb) list
15 #define MAX_DB 100
16
17 static student *db[MAX_DB];
18
19 main ()
20 {
21
FILE *fp;
22
student john;
23 char
line[BUFSIZ], * tmp ;
24
int i = 0;
(gdb)
and we are get started to debug our source!
Have no sense repeat the steps from the text based and the graphical.
We prefer to describe the debugging in "ddd". Of course, this assumption
suppose that all the programmers have only a graphical display, that generally,
have sense in these days.
The graphical interface for gdb: "xxgdb"
The fist implementation for a graphical debugger was the "xxgdb". The original
edition comes from Sun Microsystem graphical front-end.

To fix breakpoints, remove it, run the program, re-run the program,
display (that is visualize all the variables with a trace),
This debugger also uses its own ".gdbinit" file, where we can load a
specific set of debugger commands.
"xxgdb" is the classical debugger. I use for the first time in 1989
on a Sun Sparcstation 1+.
The Data Display Debugger "ddd"
A modern graphical front-end for the gdb is the "ddd" that stand for Data
Display debugger.

While "xxgbd" uses Xt libraries, "ddd" used Gtk libraries supporting
also Themes and other features. Both "xxgdb" and "ddd" are equivalent,
probably "xxgdb" will be more classical and need less memory because based
on X11 basic library, Xt, instead of Gtk.
A debugging session
Now, we will present a simple step-by-step example.
To start we need to compile the program with "-g" and then execute the
debugger with the program as a parameter.
[root@ftosx1 Chap4]# xxgdb malloc
We present here the masks:
Here we list a table about the debugger commands:
-
run, run the program. We suppose that some breakpoint are fixed in the
source, before to hit this button.
-
cont, continue the normal execution.
-
next, this execute the procedure/function without to enter inside.
-
finish, this go to the end of the function.
-
break, fix a breakpoint
-
tbreak, is a temporary break.
-
delete, delete a breakpoint.
-
up, back one level up inside the stack functions.
-
down, enter one level down in the stack functions.
-
print, print a simple variable.
-
print *, print structures or the content inside a pointer.
-
display, add a variable in the display stack in the bottom.
-
undisplay, removes a variable from the display stack.
-
show display, activate/disactivate the display stack.
-
args, show the arguments when we run the program. To run a program with
parameters, the command is: "run malloc 1 2 3".
-
locals, display local variables.
-
stack, show the stack of functions and our position.
-
search, search a symbol/variable inside the programs.
-
interrupt, stop an execution.
-
file, load a specific source file.
-
show brkpts, show breakpoints.
-
yes, answer yes in a function request.
-
no, answer no in a function request.
-
quit, exit the debugger.
Tracing the execution of a program
As we note before to trace a program we need to press or write "step" in
the debugger prompt. However, there are another command called "next" that
execute the entire function.
We can also use "up" and "down" to back up or back down in the functions
stack: main(), func1(), ...
We can also continue or interrupt a cycle.
For example, we can ask what are we located in the code:
(xxgdb) info stack
#0 main () at malloc.c:61
#1 0x4003d1fa in __libc_start_main () from /lib/libc.so.6
(xxgdb)
Or we may ask, what type is the following variable:
(xxgdb) whatis db[i]
type = student *
(xxgdb)
Or where is used:
(xxgdb) where db[i]
#0 main () at malloc.c:61
#1 0x4003d1fa in __libc_start_main () from /lib/libc.so.6
(xxgdb)
How to set up breakpoints
To set breakpoint is quite simple. We simply need to go to the code and
fix a breakpoint pressing the button "break".
How to print data in the debugger
There are two basic:
"xxgdb" supports also the possibility to select a variable and then print
the relative button.
Some hints to debug graphical programs.
Debug a text based program is quite simple but debug a graphical programs
is another thing. For example, in 1989 to debug Microsoft Windows 2.11
applications was necessary to use two monitors, one of the debugger and
one to display the program.
In UNIX and Linux, this is generally never was necessary. To debug a
text-based program like "mc" Midnight Commander or other we need simply
a good debugger. Also to debug a graphical C application, in Motif generally
we need to know only some debugger techniques as we introduce here.
However, to debug a C++ based graphical application, like a Qt application,
generally is necessary to use a MessageBox. Qt for example includes the
QtMessageBox. For debugging purpose is just perfect.
C++ debugging is more complex, but not impossible. It is simply a little
more complicated, basically in the sense that C is compared with C++.
Exercises
-
Download a C source and try to understand the C Data Declaration and the
logical operators
Test
-
What is a debugger ?
-
Why was invented ?
-
Any compiled program would be debuggable ?
-
Would be necessary to add some special flag at compilation time to debug
the program ?
-
Would be possible to install themes with "ddd" ?
-
What are "ddd" and "xxgdb" program about the gdb ?
-
Would be possible to restart the debugging process after we start ?
-
Is possible to print a structure using a debugger ?
-
What is the core file ?
-
List seven common buttons or instructions in debugger ?
-
Describe a common debugging session.
-
What is the difference between "step" and "next" ?
-
What does "down" and "up" commands ?
-
What does the "display" debugger command ?
-
Where we get the program output ?
-
How we can add arguments when we debug a program ?
-
Is possible to know the data type from the debugger ?
-
What does the "stack" command, inside a debugger session ?
-
Is possible to edit the source from the debugger ?
-
Is possible to modify and recompile the source from the debugger ?
-
What results we will get ... if we are compiling a binary and debuggin
the same binary? It is possible ?
-
What is the Linux command that set the filesize for a core ?
-
What company or organization develop the "gdb" ?
-
What happens if we built a binary with a "i386-redhat-linux" compiler and
try to debug with a "i386-ftosx-linux" compiler ? What is the relevant
element in the discussion ?
-
How is the normal mode to debug a graphical application ?
-
Is possible to debug a Tcl source ?
Consult the answers
Check the Interactive Exam Cram Programming:
Internet
Resources for this Chapter.