Debugging with GDB (September 2007)

Chapter 7: Examining Source Files 57
7 Examining Source Files
GDB can print parts of the s ource code of your program, since the debugging information
recorded in the program tells GDB what source files were used to build it. When your
program stops, GDB spontaneously prints the line where it stopped. Likewise, when you
select a stack frame (see Section 6.5 [Selecting a frame], page 53), GDB prints the line where
execution in that frame has stopped. You can print other portions of source files by explicit
command.
You can invoke GDB from its gnu Emacs interface to view the source code see Chapter 19
[Using GDB under gnu Emacs], page 255.
7.1 Printing source lines
To print lines from a s ource file, use the list command (abbreviated l). By default, ten
lines are printed. There are several ways to specify what part of the file you want to print.
The following forms of the list command are used:
list linenum
Prints lines centered around line number linenum in the current source file.
list function
Prints lines centered around the beginning of function function.
list Prints more lines. The list command prints lines following the lines printed
by a previously executed list command. If the command prior to executing
a list just printed the stack frame, then the list command only prints the
lines around that line.
list - Prints lines just b efore the lines last printed.
By default, GDB prints ten source lines with any of these forms of the list command.
The number of lines printed by GDB can be set by the set listsize command. The
following two forms are supported:
set listsize count
Makes the list command display count source lines (unless the list argument
explicitly specifies some other number).
show listsize
Displays the number of lines that list prints.
Repeating a list command with
h
RET
i
discards the argument, so it is equivalent to
typing just list. This is more useful than listing the same lines again. An exception is
made for an argument of -’; that argument is preserved in repetition so that each repetition
moves up in the source file.
In general, the list command expects you to supply zero, one or two linespecs.
Linespecs specify source lines. There are several ways of writing them, but the most
common way is to specify some source line.
The following arguments can be given to the list command: