[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
The usual way to examine data in your program is with the print
command (abbreviated p
), or its synonym inspect
. It
evaluates and prints the value of an expression of the language your
program is written in (see section Using GDB with Different Languages).
print expr
print /f
expr
print
print /f
If you omit expr, GDB displays the last value again (from the value history; see section Value History). This allows you to conveniently inspect the same value in an alternative format.
A more low-level way of examining data is with the x
command.
It examines data in memory at a specified address and prints it in a
specified format. See section Examining Memory.
If you are interested in information about types, or about how the
fields of a struct or a class are declared, use the ptype exp
command rather than print
. See section Examining the Symbol Table.
8.1 Expressions 8.2 Program Variables Program variables 8.3 Artificial Arrays Artificial arrays 8.4 Output Formats Output formats 8.5 Examining Memory Examining memory 8.6 Automatic Display Automatic display 8.7 Print Settings Print settings 8.8 Value History Value history 8.9 Convenience Variables Convenience variables 8.10 Registers 8.11 Floating Point Hardware Floating point hardware 8.12 Vector Unit 8.13 Operating System Auxiliary Information Auxiliary data provided by operating system 8.14 Memory Region Attributes Memory region attributes 8.15 Copy Between Memory and a File Copy between memory and a file 8.16 How to Produce a Core File from Your Program Cause a program dump its core 8.17 Character Sets Debugging programs that use a different character set than GDB does 8.18 Caching Data of Remote Targets Data caching for remote targets
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
print
and many other GDB commands accept an expression and
compute its value. Any kind of constant, variable or operator defined
by the programming language you are using is valid in an expression in
GDB. This includes conditional expressions, function calls,
casts, and string constants. It also includes preprocessor macros, if
you compiled your program to include this information; see
4.1 Compiling for Debugging.
GDB supports array constants in expressions input by
the user. The syntax is {element, element
...}. For example,
you can use the command print {1, 2, 3}
to build up an array in
memory that is malloc
ed in the target program.
Because C is so widespread, most of the expressions shown in examples in this manual are in C. See section Using GDB with Different Languages, for information on how to use expressions in other languages.
In this section, we discuss operators that you can use in GDB expressions regardless of your programming language.
Casts are supported in all languages, not just in C, because it is so useful to cast a number into a pointer in order to examine a structure at that address in memory.
GDB supports these operators, in addition to those common to programming languages:
@
::
{type} addr
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
The most common kind of expression to use is the name of a variable in your program.
Variables in expressions are understood in the selected stack frame (see section Selecting a Frame); they must be either:
or
This means that in the function
foo (a) int a; { bar (a); { int b = test (); bar (b); } } |
file::variable function::variable |
(gdb) p 'f2.c'::x |
No symbol "foo" in current context. |
char var0[] = "A"; signed char var1[] = "A"; |
(gdb) print var0 $1 = "A" (gdb) print var1 $2 = {65 'A', 0 '\0'} |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
It is often useful to print out several successive objects of the same type in memory; a section of an array, or an array of dynamically determined size for which only a pointer exists in the program.
You can do this by referring to a contiguous span of memory as an artificial array, using the binary operator `@'. The left operand of `@' should be the first element of the desired array and be an individual object. The right operand should be the desired length of the array. The result is an array value whose elements are all of the type of the left argument. The first element is actually the left argument; the second element comes from bytes of memory immediately following those that hold the first element, and so on. Here is an example. If a program says
int *array = (int *) malloc (len * sizeof (int)); |
p *array@len |
(gdb) p/x (short[2])0x12345678 $1 = {0x1234, 0x5678} |
(gdb) p/x (short[])0x12345678 $2 = {0x1234, 0x5678} |
set $i = 0 p dtab[$i++]->fv RET RET ... |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
By default, GDB prints a value according to its data type. Sometimes this is not what you want. For example, you might want to print a number in hex, or a pointer in decimal. Or you might want to view data in memory at a certain address as a character string or as an instruction. To do these things, specify an output format when you print a value.
The simplest use of output formats is to say how to print a value
already computed. This is done by starting the arguments of the
print
command with a slash and a format letter. The format
letters supported are:
x
d
u
o
t
(5)
a
Print as an address, both absolute in hexadecimal and as an offset from the nearest preceding symbol. You can use this format used to discover where (in what function) an unknown address is located:
(gdb) p/a 0x54320 $3 = 0x54320 <_initialize_vx+396> |
p/x $pc |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
You can use the command x
(for "examine") to examine memory in
any of several formats, independently of your program's data types.
x/nfu
addr
x addr
x
x
command to examine memory.
n, f, and u are all optional parameters that specify how much memory to display and how to format it; addr is an expression giving the address where you want to start displaying memory. If you use defaults for nfu, you need not type the slash `/'. Several commands set convenient defaults for addr.
print
(`x', `d', `u', `o', `t', `a', `c',
`f', `s'), and in addition `i' (for machine instructions).
The default is `x' (hexadecimal) initially. The default changes
each time you use either x
or print
.
b
h
w
g
Each time you specify a unit size with x
, that size becomes the
default unit the next time you use x
. (For the `s' and
`i' formats, the unit size is ignored and is normally not written.)
info breakpoints
(to
the address of the last breakpoint listed), info line
(to the
starting address of a line), and print
(if you use it to display
a value from memory).
For example, `x/3uh 0x54320' is a request to display three halfwords
(h
) of memory, formatted as unsigned decimal integers (`u'),
starting at address 0x54320
. `x/4xw $sp' prints the four
words (`w') of memory above the stack pointer (here, `$sp';
see section Registers) in hexadecimal (`x').
Since the letters indicating unit sizes are all distinct from the letters specifying output formats, you do not have to remember whether unit size or format comes first; either order works. The output specifications `4xw' and `4wx' mean exactly the same thing. (However, the count n must come first; `wx4' does not work.)
Even though the unit size u is ignored for the formats `s'
and `i', you might still want to use a count n; for example,
`3i' specifies that you want to see three machine instructions,
including any operands. For convenience, especially when used with
the display
command, the `i' format also prints branch delay
slot instructions, if any, beyond the count specified, which immediately
follow the last instruction that is within the count. The command
disassemble
gives an alternative way of inspecting machine
instructions; see Source and Machine Code.
All the defaults for the arguments to x
are designed to make it
easy to continue scanning memory with minimal specifications each time
you use x
. For example, after you have inspected three machine
instructions with `x/3i addr', you can inspect the next seven
with just `x/7'. If you use RET to repeat the x
command,
the repeat count n is used again; the other arguments default as
for successive uses of x
.
The addresses and contents printed by the x
command are not saved
in the value history because there is often too much of them and they
would get in the way. Instead, GDB makes these values available for
subsequent use in expressions as values of the convenience variables
$_
and $__
. After an x
command, the last address
examined is available for use in expressions in the convenience variable
$_
. The contents of that address, as examined, are available in
the convenience variable $__
.
If the x
command has a repeat count, the address and contents saved
are from the last memory unit printed; this is not the same as the last
address printed if several units were printed on the last line of output.
When you are debugging a program running on a remote target machine
(see section 17. Debugging Remote Programs), you may wish to verify the program's image in the
remote machine's memory against the executable file you downloaded to
the target. The compare-sections
command is provided for such
situations.
compare-sections [section-name]
"qCRC"
remote request.
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
If you find that you want to print the value of an expression frequently (to see how it changes), you might want to add it to the automatic display list so that GDB prints its value each time your program stops. Each expression added to the list is given a number to identify it; to remove an expression from the list, you specify that number. The automatic display looks like this:
2: foo = 38 3: bar[5] = (struct hack *) 0x3804 |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
GDB provides the following ways to control how arrays, structures, and symbols are printed.
These settings are useful for debugging programs in any language:
set print address
set print address on
GDB prints memory addresses showing the location of stack
traces, structure values, pointer values, breakpoints, and so forth,
even when it also displays the contents of those addresses. The default
is on
. For example, this is what a stack frame display looks like with
set print address on
:
(gdb) f #0 set_quotes (lq=0x34c78 "<<", rq=0x34c88 ">>") at input.c:530 530 if (lquote != def_lquote) |
(gdb) set print addr off (gdb) f #0 set_quotes (lq="<<", rq="">>") at input.c:530 530 if (lquote != def_lquote) |
(gdb) set print symbol-filename on (gdb) p/a ptt $4 = 0xe008 <t in hi2.c> |
#1 0x08048361 in call_me (i=3, s=..., ss=0xbf8d508c, u=..., e=green) at frame-args.c:23 |
#1 0x08048361 in call_me (i=..., s=..., ss=..., u=..., e=...) at frame-args.c:23 |
$1 = { next = 0x0, flags = { sweet = 1, sour = 1 }, meat = 0x54 "Pork" } |
$1 = {next = 0x0, flags = {sweet = 1, sour = 1}, \ meat = 0x54 "Pork"} |
typedef enum {Tree, Bug} Species; typedef enum {Big_tree, Acorn, Seedling} Tree_forms; typedef enum {Caterpillar, Cocoon, Butterfly} Bug_forms; struct thing { Species it; union { Tree_forms tree; Bug_forms bug; } form; }; struct thing foo = {Tree, {Acorn}}; |
$1 = {it = Tree, form = {tree = Acorn, bug = Cocoon}} |
$1 = {it = Tree, form = {...}} |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Values printed by the print
command are saved in the GDB
value history. This allows you to refer to them in other expressions.
Values are kept until the symbol table is re-read or discarded
(for example with the file
or symbol-file
commands).
When the symbol table changes, the value history is discarded,
since the values may contain pointers back to the types defined in the
symbol table.
The values printed are given history numbers by which you can
refer to them. These are successive integers starting with one.
print
shows you the history number assigned to a value by
printing `$num = ' before the value; here num is the
history number.
To refer to any previous value, use `$' followed by the value's
history number. The way print
labels its output is designed to
remind you of this. Just $
refers to the most recent value in
the history, and $$
refers to the value before that.
$$n
refers to the nth value from the end; $$2
is the value just prior to $$
, $$1
is equivalent to
$$
, and $$0
is equivalent to $
.
For example, suppose you have just printed a pointer to a structure and want to see the contents of the structure. It suffices to type
p *$ |
p *$.next |
print x set x=5 |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
GDB provides convenience variables that you can use within GDB to hold on to a value and refer to it later. These variables exist entirely within GDB; they are not part of your program, and setting a convenience variable has no direct effect on further execution of your program. That is why you can use them freely.
Convenience variables are prefixed with `$'. Any name preceded by `$' can be used for a convenience variable, unless it is one of the predefined machine-specific register names (see section Registers). (Value history references, in contrast, are numbers preceded by `$'. See section Value History.)
You can save a value in a convenience variable with an assignment expression, just as you would set a variable in your program. For example:
set $foo = *object_ptr |
set $i = 0 print bar[$i++]->contents |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
You can refer to machine register contents, in expressions, as variables
with names starting with `$'. The names of registers are different
for each machine; use info registers
to see the names used on
your machine.
info registers
info all-registers
info registers regname
...
GDB has four "standard" register names that are available (in
expressions) on most machines--whenever they do not conflict with an
architecture's canonical mnemonics for registers. The register names
$pc
and $sp
are used for the program counter register and
the stack pointer. $fp
is used for a register that contains a
pointer to the current stack frame, and $ps
is used for a
register that contains the processor status. For example,
you could print the program counter in hex with
p/x $pc |
x/i $pc |
set $sp += 4 |
(gdb) print $xmm1 $1 = { v4_float = {0, 3.43859137e-038, 1.54142831e-044, 1.821688e-044}, v2_double = {9.92129282474342e-303, 2.7585945287983262e-313}, v16_int8 = "\000\000\000\000\3706;\001\v\000\000\000\r\000\000", v8_int16 = {0, 0, 14072, 315, 11, 0, 13, 0}, v4_int32 = {0, 20657912, 11, 13}, v2_int64 = {88725056443645952, 55834574859}, uint128 = 0x0000000d0000000b013b36f800000000 } |
(gdb) set $xmm1.uint128 = 0x000000000000000000000000FFFFFFFF |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Depending on the configuration, GDB may be able to give you more information about the status of the floating point hardware.
info float
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Depending on the configuration, GDB may be able to give you more information about the status of the vector unit.
info vector
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
GDB provides interfaces to useful OS facilities that can help you debug your program.
When GDB runs on a Posix system (such as GNU or Unix
machines), it interfaces with the inferior via the ptrace
system call. The operating system creates a special sata structure,
called struct user
, for this interface. You can use the
command info udot
to display the contents of this data
structure.
info udot
Display the contents of the struct user
maintained by the OS
kernel for the program being debugged. GDB displays the
contents of struct user
as a list of hex numbers, similar to
the examine
command.
Some operating systems supply an auxiliary vector to programs at startup. This is akin to the arguments and environment that you specify for a program, but contains a system-dependent variety of binary values that tell system libraries important details about the hardware, operating system, and process. Each value's purpose is identified by an integer tag; the meanings are well-known but system-specific. Depending on the configuration and operating system facilities, GDB may be able to show you this information. For remote targets, this functionality may further depend on the remote stub's support of the `qXfer:auxv:read' packet, see qXfer auxiliary vector read.
info auxv
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Memory region attributes allow you to describe special handling required by regions of your target's memory. GDB uses attributes to determine whether to allow certain types of memory accesses; whether to use specific width accesses; and whether to cache target memory. By default the description of memory regions is fetched from the target (if the current target supports this), but the user can override the fetched regions.
Defined memory regions can be individually enabled and disabled. When a memory region is disabled, GDB uses the default attributes when accessing memory in that region. Similarly, if no memory regions have been defined, GDB uses the default attributes when accessing all memory.
When a memory region is defined, it is given a number to identify it; to enable, disable, or remove a memory region, you specify that number.
mem lower
upper
attributes
...
mem auto
delete mem nums
...
disable mem nums
...
enable mem nums
...
info mem
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
While these attributes prevent GDB from performing invalid memory accesses, they do nothing to prevent the target system, I/O DMA, etc. from accessing memory.
ro
wo
rw
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
8
16
32
64
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
cache
nocache
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
set mem inaccessible-by-default [on|off]
on
is specified, make GDB treat memory not
explicitly described by the memory ranges as non-existent and refuse accesses
to such memory. The checks are only performed if there's at least one
memory range defined. If off
is specified, make GDB
treat the memory not explicitly described by the memory ranges as RAM.
The default value is on
.
show mem inaccessible-by-default
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
You can use the commands dump
, append
, and
restore
to copy data between target memory and a file. The
dump
and append
commands write data to a file, and the
restore
command reads data from a file back into the inferior's
memory. Files may be in binary, Motorola S-record, Intel hex, or
Tektronix Hex format; however, GDB can only append to binary
files.
dump [format] memory filename
start_addr
end_addr
dump [format] value filename
expr
The format parameter may be any one of:
binary
ihex
srec
tekhex
GDB uses the same definitions of these formats as the GNU binary utilities, like `objdump' and `objcopy'. If format is omitted, GDB dumps the data in raw binary form.
append [binary] memory filename
start_addr
end_addr
append [binary] value filename
expr
restore filename [binary] bias
start
end
restore
command can automatically recognize any known BFD
file format, except for raw binary. To restore a raw binary file you
must specify the optional keyword binary
after the filename.
If bias is non-zero, its value will be added to the addresses contained in the file. Binary files always start at address zero, so they will be restored at address bias. Other bfd files have a built-in location; they will be restored at offset bias from that location.
If start and/or end are non-zero, then only data between file offset start and file offset end will be restored. These offsets are relative to the addresses in the file, before the bias argument is applied.
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
A core file or core dump is a file that records the memory image of a running process and its process status (register values etc.). Its primary use is post-mortem debugging of a program that crashed while it ran outside a debugger. A program that crashes automatically produces a core file, unless this feature is disabled by the user. See section 15.1 Commands to Specify Files, for information on invoking GDB in the post-mortem debugging mode.
Occasionally, you may wish to produce a core file of the program you are debugging in order to preserve a snapshot of its state. GDB has a special command for that.
generate-core-file [file]
gcore [file]
Note that this command is implemented only for some systems (as of this writing, GNU/Linux, FreeBSD, Solaris, Unixware, and S390).
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
If the program you are debugging uses a different character set to represent characters and strings than the one GDB uses itself, GDB can automatically translate between the character sets for you. The character set GDB uses we call the host character set; the one the inferior program uses we call the target character set.
For example, if you are running GDB on a GNU/Linux system, which
uses the ISO Latin 1 character set, but you are using GDB's
remote protocol (see section 17. Debugging Remote Programs) to debug a program
running on an IBM mainframe, which uses the EBCDIC character set,
then the host character set is Latin-1, and the target character set is
EBCDIC. If you give GDB the command set
target-charset EBCDIC-US
, then GDB translates between
EBCDIC and Latin 1 as you print character or string values, or use
character and string literals in expressions.
GDB has no way to automatically recognize which character set
the inferior program uses; you must tell it, using the set
target-charset
command, described below.
Here are the commands for controlling GDB's character set support:
set target-charset charset
Set the current target character set to charset. We list the
character set names GDB recognizes below, but if you type
set target-charset
followed by TAB
TAB, GDB will
list the target character sets it supports.
set host-charset charset
Set the current host character set to charset.
By default, GDB uses a host character set appropriate to the
system it is running on; you can override that default using the
set host-charset
command.
GDB can only use certain character sets as its host character
set. We list the character set names GDB recognizes below, and
indicate which can be host character sets, but if you type
set target-charset
followed by TAB
TAB, GDB will
list the host character sets it supports.
set charset charset
Set the current host and target character sets to charset. As
above, if you type set charset
followed by TAB
TAB,
GDB will list the name of the character sets that can be used
for both host and target.
show charset
Show the names of the current host and target charsets.
show host-charset
Show the name of the current host charset.
show target-charset
Show the name of the current target charset.
GDB currently includes support for the following character sets:
ASCII
Seven-bit U.S. ASCII. GDB can use this as its host character set.
ISO-8859-1
The ISO Latin 1 character set. This extends ASCII with accented characters needed for French, German, and Spanish. GDB can use this as its host character set.
EBCDIC-US
IBM1047
Variants of the EBCDIC character set, used on some of IBM's mainframe operating systems. (GNU/Linux on the S/390 uses U.S. ASCII.) GDB cannot use these as its host character set.
Note that these are all single-byte character sets. More work inside GDB is needed to support multi-byte or variable-width character encodings, like the UTF-8 and UCS-2 encodings of Unicode.
Here is an example of GDB's character set support in action. Assume that the following source code has been placed in the file `charset-test.c':
#include <stdio.h> char ascii_hello[] = {72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 10, 0}; char ibm1047_hello[] = {200, 133, 147, 147, 150, 107, 64, 166, 150, 153, 147, 132, 90, 37, 0}; main () { printf ("Hello, world!\n"); } |
$ gcc -g charset-test.c -o charset-test $ gdb -nw charset-test GNU gdb 2001-12-19-cvs Copyright 2001 Free Software Foundation, Inc. ... (gdb) |
(gdb) show charset The current host and target character set is `ISO-8859-1'. (gdb) |
(gdb) set charset ASCII (gdb) show charset The current host and target character set is `ASCII'. (gdb) |
(gdb) print ascii_hello $1 = 0x401698 "Hello, world!\n" (gdb) print ascii_hello[0] $2 = 72 'H' (gdb) |
(gdb) print '+' $3 = 43 '+' (gdb) |
(gdb) print ibm1047_hello $4 = 0x4016a8 "\310\205\223\223\226k@\246\226\231\223\204Z%" (gdb) print ibm1047_hello[0] $5 = 200 '\310' (gdb) |
(gdb) set target-charset ASCII EBCDIC-US IBM1047 ISO-8859-1 (gdb) set target-charset |
(gdb) set target-charset IBM1047 (gdb) show charset The current host character set is `ASCII'. The current target character set is `IBM1047'. (gdb) print ascii_hello $6 = 0x401698 "\110\145%%?\054\040\167?\162%\144\041\012" (gdb) print ascii_hello[0] $7 = 72 '\110' (gdb) print ibm1047_hello $8 = 0x4016a8 "Hello, world!\n" (gdb) print ibm1047_hello[0] $9 = 200 'H' (gdb) |
(gdb) print '+' $10 = 78 '+' (gdb) |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
GDB can cache data exchanged between the debugger and a remote target (see section 17. Debugging Remote Programs). Such caching generally improves performance, because it reduces the overhead of the remote protocol by bundling memory reads and writes into large chunks. Unfortunately, GDB does not currently know anything about volatile registers, and thus data caching will produce incorrect results when volatile registers are in use.
set remotecache on
set remotecache off
ON
, use data
caching. By default, this option is OFF
.
show remotecache
info dcache
[ <<] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Please send FSF & GNU inquiries & questions to gnu@gnu.org. There are also other ways to contact the FSF.
These pages are maintained by the GDB developers.
Copyright Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.
This document was generated by GDB Administrator on March, 27 2008 using texi2html