Debugging with GDB Manual The GNU Source-Level Debugger (769148-001, March 2014)

7 Examining Source Files
GDB can print parts of the source 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
“Selecting a frame” (page 51)), 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
(page 224).
Printing source lines
To print lines from a source 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 before 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 RET 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:
list linespec Print lines centered around the line specified by linespec.
list first,last Print lines from first to last. Both arguments must be linespecs.
list, last Print lines ending with last.
list first, Print lines starting with first.
list + Print lines just after the lines last printed.
list - Print lines just before the lines last printed.
list As described in the preceding table.
A single source line can be specified in the following ways:
number Specifies line number of the current source file. When a list
command has two linespecs, this refers to the same source file as the
first linespec.
54 Examining Source Files