The gdb tool is a symbolic debugger. When you compile a program with the
-g flag, the symbol tables are retained, and you can use a symbolic debugger
to track program bugs. The basic technique is to invoke gdb after a core
dump (which involves taking a snapshot of the memory used by a program
that has crashed) and get a stack trace. The stack trace indicates the source
line where the core dump occurred and the functions that were called to reach
that line. Often, this is enough to identify a problem. It isn’t the limit of gdb,
though.
gdb also provides an environment for debugging programs interactively.
Invoking gdb with a program enables you to set breakpoints, examine the
values of variables, and monitor variables. If you suspect a problem near a
line of code, you can set a breakpoint at that line and run gdb. When the line
is reached, execution is interrupted. You can check variable values, examine
the stack trace, and observe the program’s environment. You can single-step
through the program to check values. You can resume execution at any point.
By using breakpoints, you can discover many bugs in code.
A graphical X Window interface to gdb is called the Data Display Debugger,
or ddd.
Using the GNU C Compiler
If you elected to install the development tools package when you installed
Ubuntu (or perhaps later on, using synaptic), you should have the GNU C
compiler (gcc). Many different options are available for the GNU C
compiler, and many of them are similar to those of the C and C++ compilers
that are available on other UNIX systems. Look at the man page or
information file for gcc for a full list of options and descriptions.
NOTE
The GNU C compiler is a part of the GNU Compiler Collection, which also
includes compilers for several other languages.
When you build a C program using gcc, the compilation process takes place
in several steps:
- First, the C preprocessor parses the file. To do so, it sequentially reads the
lines, includes header files, and performs macro replacement. - The compiler parses the modified code to determine whether the correct
syntax is used. In the process, it builds a symbol table and creates an
intermediate object format. Most symbols have specific memory