Debugging Programs with gdb 399
A function can be specified as part of the variable name in the form
function::variableto reference a variable local to a specific routine, for example,
(gdb) print main::i Display contents of i in main
$4 = 25
(gdb) set var main::i=0 Set value of i in main
Note that attempting to set a variable in an inactive function (that is, a function that is
not either currently executing or waiting for another function to return to continue its
own execution) is an error and results in the following message:
No symbol "var" in current context.
Global variables can be directly referenced as 'file’::var.This forces gdbto access an
external variable as defined in the file fileand ignore any local variable of the same name
in the current function.
Structure and union members can be accessed using standard C syntax. If datePtris
a pointer to a date structure,print datePtr->yearprints the year member of the
structure pointed to by datePtr.
Referencing a structure or union without a member causes the contents of the entire
structure or union to be displayed.
You can force gdbto display a variable in a different format, for example hexadeci-
mal, by following the printcommand with a /and a letter that specifies the format to
use. Many gdbcommands can be abbreviated with a single letter. In the following exam-
ple, the abbreviation for the printcommand, which is p, is used:
(gdb) set var i=35 Set i to 35
(gdb) p /x i Display i in hexadecimal
$1 = 0x23
Source File Display
gdbprovides several commands that give you access to the source files.This enables you
to debug the program without having to reference a source listing or open your source
files in other windows.
As mentioned earlier,gdbmaintains an idea of what the current line and file are.
You’ve seen how you can display the area around the current line with the listcom-
mand, which can be abbreviated as l. Each time you subsequently type the listcom-
mand (or more simply, just press the Enteror Return key), the next 10 lines from the file
are displayed.This value of 10 is the default and can be set to any value by using the
listsizecommand.
If you want to display a range of lines, you can specify the starting and ending line
numbers, separated by a comma, as follows:
(gdb) list 10,15 List lines 10 through 15