User guide

Chapter 5. Debugging
54
not only the characteristics of each individual elements in the structure, but the morphology of the
structure as well.
GDB requires the following items in order to fully function:
Debug Information
Much of GDB's operations rely on a program's debug information. While this information generally
comes from compilers, much of it is necessary only while debugging a program, i.e. it is not
used during the program's normal execution. For this reason, compilers do not always make that
information available by default — GCC, for instance, must be explicitly instructed to provide this
debugging information with the -g flag.
To make full use of GDB's capabilities, it is highly advisable to make the debug information
available first to GDB. GDB can only be of very limited use when run against code with no
available debug information.
Source Code
One of the most useful features of GDB (or any other debugger) is the ability to associate events
and circumstances in program execution with their corresponding location in source code. This
location normally refers to a specific line or series of lines in a source file. This, of course, would
require that a program's source code be available to GDB at debug time.
5.2.1. Simple GDB
GDB literally contains dozens of commands. This section describes the most fundamental ones.
br (breakpoint)
The breakpoint command instructs GDB to halt execution upon reaching a specified point in the
execution That point can be specified a number of ways, but the most common are just as the line
number in the source file, or the name of a function. Any number of breakpoints can be in effect
simultaneously. This is frequently the first command issued after starting GDB.
r (run)
The run command starts the execution of the program. If run is executed with any arguments,
those arguments are passed on to the executable as if the program has been started normally.
Users normally issue this command after setting breakpoints.
Before an executable is started, or once the executable stops at, for example, a breakpoint, the state
of many aspects of the program can be inspected. The following commands are a few of the more
common ways things can be examined.
p (print)
The print command displays the value of the argument given, and that argument can be almost
anything relevant to the program. Usually, the argument is simply the name of a variable of any
complexity, from a simple single value to a structure. An argument can also be an expression valid
in the current language, including the use of program variables and library functions, or functions
defined in the program beingtested.
bt (backtrace)
The backtrace displays the chain of function calls used up until the exectuion was terminated.
This is useful for investigating serious bugs (such as segmentation faults) with elusive causes.
l (list)
When execution is stopped, the list command shows the line in the source code corresponding
to where the program stopped.