Debugging with GDB Manual The GNU Source-Level Debugger (769148-001, March 2014)
other commands also set the default address: 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 “Registers” (page 69)) 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. The command disassemble gives an alternative way of inspecting machine instructions;
see “Source and machine code” (page 56).
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.
Automatic display
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
This display shows item numbers, expressions, and their current values. As with displays you request
manually using x or print, you can specify the output format you prefer; in fact, display decides
whether to use print or x depending on how elaborate your format specification is―it uses x if
you specify a unit size, or one of the two formats ('i' and 's') that are only supported by x;
otherwise it uses print.
display expr Add the expression expr to the list of expressions to display each time
your program stops. See “Expressions” (page 58).
display does not repeat if you press RET again after using it.
display/fmt expr For fmt specifying only a display format and not a size or count, add
the expression expr to the auto-display list but arrange to display it
each time in the specified format fmt. See“Output formats” (page 60).
62 Examining Data