14-1Sample Output for the find command....................................................................198
14-2Sample Commands to Print NaT Registers...............................................................214
14List of Examples
Summary of GDB
The purpose of a debugger such as GDB is to allow you to see whatis going on “inside”
another programwhile it executes―or what another program was doing at the moment
it crashed.
GDB allows you to do the following:
•Load the executable along with any required arguments.
•Stop your program on specified blocks of code.
•Examine your program when it has stopped running due to an error.
•Change things in your program, so you can experiment with correcting the effects
of one bug and go on to learn about another.
You can use GDB to debug programs written in C, C++, and Fortran. For more
information, refer to the “Supported languages” (page 105). For more information on
supported languages, refer to the “C and C++” (page 106).
GDB can be used to debug programs written in Fortran, although it may be necessary
to refer to some variables with a trailing underscore. See “Fortran” (page 112).
This version of the manual documents WDB, implemented on HP 9000 or HP Integrity
systems running Release 11.x of the HP-UX operating system. WDB can be used to
debug code generated by the HP ANSI C, HP ANSI aC++ and HP Fortran compilers
as well as the GNU C and C++ compilers. It does not support the debugging of Pascal,
Modula-2 or Chill programs.
Free Software
GDB is free software, protected by the GNU General Public License (GPL). The GPL gives
you the freedom to copy or adapt a licensed program―but every person getting a copy
also gets with it the freedom to modify that copy (which means that they must get
access to the source code), and the freedom to distribute further copies. Typical software
companies use copyrights to limit your freedoms; the Free Software Foundation uses
the GPL to preserve these freedoms.
Fundamentally, the General Public License is a license which says that you have these
freedoms and that you cannot take these freedoms away from anyone else.
Contributors to GDB
Richard Stallman was the original author of GDB, and of many other GNU programs.
Many others have contributed to its development. This section attempts to credit major
contributors. One of the virtues of free software is that everyone is free to contribute
to it; with regret, we cannot actually acknowledge everyone here. The file 'ChangeLog'
in the GDB distribution approximates a blow-by-blow account.
Changes much prior to version 2.0 are lost in the mists of time.
Free Software15
Plea: Additions to this section are particularly welcome. If you or your friends (or
enemies, to be evenhanded) have been unfairly omitted from this list, we would like
to add your names!
So that they may not regard their many labors as thankless, we particularly thank those
who shepherdedGDB through major releases: Andrew Cagney (release 5.0); Jim Blandy
(release 4.18); Jason Molenda (release 4.17); Stan Shebs (release 4.14); Fred Fish (releases
4.16, 4.15, 4.13, 4.12, 4.11, 4.10, and 4.9); Stu Grossman and John Gilmore (releases 4.8,
4.7, 4.6, 4.5, and 4.4); John Gilmore (releases 4.3, 4.2, 4.1, 4.0, and 3.9); Jim Kingdon
(releases 3.5, 3.4, and 3.3); and Randy Smith (releases 3.2, 3.1, and 3.0).
Richard Stallman,assisted at various times by Peter TerMaat, Chris Hanson, and Richard
Mlynarik, handled releases through 2.8.
Michael Tiemann is the author of most of the GNU C++ support in GDB, with significant
additional contributions from Per Bothner. James Clark wrote the GNU C++ demangler.
Early work on C++ was by Peter TerMaat (who also did much general update work
leading to release 3.0).
GDB 4 uses the BFD subroutine library to examine multiple object-file formats; BFD
was a joint project of David V. Henkel-Wallace, Rich Pixley, Steve Chamberlain, and
John Gilmore.
David Johnson wrote the original COFF support; Pace Willison did the original support
for encapsulated COFF.
Brent Benson of Harris Computer Systems contributed DWARF 2 support.
Adam de Boor andBradley Daviscontributed theISI OptimumV support.Per Bothner,
Noboyuki Hikichi,and Alessandro Forin contributedMIPS support. Jean-Daniel Fekete
contributed Sun 386i support. Chris Hanson improved the HP 9000 support. Noboyuki
Hikichi and Tomoyuki Hasei contributed Sony/News OS 3 support. David Johnson
contributed Encore Umaxsupport. Jyrki Kuoppala contributed Altos 3068 support. Jeff
Law contributed HP PA and SOM support. Keith Packard contributed NS32K support.
Doug Rabson contributed Acorn Risc Machine support. Bob Rusk contributed Harris
Nighthawk CX-UX support. Chris Smith contributed Convex support (and Fortran
debugging). JonathanStone contributed Pyramid support. Michael Tiemann contributed
SPARC support. Tim Tucker contributed support for the Gould NP1 and Gould
Powernode. Pace Willison contributed Intel 386 support. Jay Vosburgh contributed
Symmetry support.
Andreas Schwab contributed M68K Linux support.
Rich Schaefer and Peter Schauer helped with support of SunOS shared libraries.
Jay Fenlason and Roland McGrath ensured that GDB and GAS agree about several
machine instruction sets.
Patrick Duval, Ted Goldstein, Vikram Koka and Glenn Engel helped develop remote
debugging. IntelCorporation, WindRiver Systems, AMD, and ARM contributed remote
debugging modules for the i960, VxWorks, A29K UDI, and RDI targets, respectively.
16
Brian Fox is the author of the readline libraries providing command-line editing and
command history.
Andrew Beers of SUNY Buffalo wrote the language-switching code, the Modula-2
support, and contributed the Languages chapter of this manual.
Fred Fish wrote most of the support for Unix System Vr4. He also enhanced the
command-completion support to cover C++ overloaded symbols.
Hitachi America, Ltd. sponsored the support for H8/300, H8/500, and Super-H
processors.
NEC sponsored the support for the v850, Vr4xxx, and Vr5xxx processors.
Mitsubishi sponsored the support for D10V, D30V, and M32R/D processors.
Toshiba sponsored the support for the TX39 Mips processor.
Matsushita sponsored the support for the MN10200 and MN10300 processors.
Fujitsu sponsored the support for SPARClite and FR30 processors.
Kung Hsu, Je Law, and Rick Sladkey added support for hardware watchpoints.
Michael Snyder added support for tracepoints.
Stu Grossman wrote gdbserver.
Jim Kingdon, Peter Schauer, Ian Taylor, and Stu Grossman made nearly innumerable
bug fixes and cleanups throughout GDB.
The following people at the Hewlett-Packard Company contributed support for the
PA-RISC 2.0 architecture, HP-UX 10.20, 10.30, and 11.x (narrow mode), HP's
implementation of kernel threads, HP's aC++ compiler, and the terminal user interface:
Ben Krepp, Richard Title, John Bishop, Susan Macchia, Kathy Mann, Satish Pai, India
Paul, Steve Rehrauer, and Elena Zannoni. Kim Haase, Rosario de la Torre, Alex McKale,
Michael Coulter, Carl Burch, Bharath Chndramohan, Diwakar Nag, Muthuswami,
Dennis Handly, Subash Babu and Dipshikha Basu provided HP-specific information
in this manual.
Cygnus Solutions has sponsored GDB maintenance and much of its development since
1991. Cygnus engineers who have worked on GDB full time include Mark Alexander,
Jim Blandy, Per Bothner, Kevin Buettner, Edith Epstein, Chris Faylor, Fred Fish, Martin
Hunt, Jim Ingham, John Gilmore, Stu Grossman, Kung Hsu, Jim Kingdon, John Metzler,
Fernando Nasser, Georey Noer, Dawn Perchik, Rich Pixley, Zdenek Radouch, Keith
Seitz, Stan Shebs, David Taylor, and Elena Zannoni. In addition, Dave Brolley, Ian
Carmichael, Steve Chamberlain, Nick Clifton, JT Conklin, Stan Cox, DJ Delorie, Ulrich
Drepper, Frank Eigler, Doug Evans, Sean Fagan, David Henkel-Wallace, Richard
Henderson, Jeff Holcomb, Jeff Law, Jim Lemke, Tom Lord, Bob Manson, Michael
Meissner, Jason Merrill, Catherine Moore, Drew Moseley, Ken Raeburn, Gavin
Romig-Koch, Rob Savoye, Jamie Smith, Mike Stump, Ian Taylor, Angela Thomas,
Michael Tiemann, Tom Tromey, Ron Unrau, Jim Wilson, and David Zuhn have made
contributions both large and small.
Contributors to GDB17
18
1 A Sample GDB Session
This chapter describes the most common GDB commands with the help of an example.
The following topics are discussed:
•Loading the Executable
•Setting the Display Width
•Setting Breakpoints
•Running the Executable under GDB
•Stepping to the next line
•Stepping into a Subroutine
•Examining the Stack
•Printing Variable Values
•Listing the Source Code
•Setting Variable Values During a Debug Session
In this sample session, we emphasize user input like this: input, to make it easier to
pick out from the surrounding output.
One of the preliminary versions of GNU m4 (a generic macro processor) exhibits the
following bug: sometimes, when we change its quote strings from the default, the
commands used to capture one macro definition within another stop working. In the
following short m4 session, we define a macro foo which expands to 0000; we then
use the m4 built-in defn to define bar as the same thing. However, when we
change the open quote string to <QUOTE> and the close quote string to <UNQUOTE>,
the same procedure fails to define a new synonym baz:
$ cd gnu/m4 //change your current directory to the location where the m4 executable is stored.
$ ./m4 //run the m4 application
define(foo,0000)
foo
0000
define (bar,defn('foo'))
bar
0000
changequote(<QUOTE>,<UNQUOTE>)
define(baz,defn(<QUOTE>foo<UNQUOTE>))
baz
C-d
m4: End of input: 0: fatal error: EOF in string
1.1 Loading the Executable
Let us use GDB to try to see what is going on.
1.1 Loading the Executable19
$ (gdb) m4
HP gdb 3.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 3.0 (based on GDB ) is covered by the
GNU General Public License. Type "show copying" to see the conditions to
change it and/or distribute copies. Type "show warranty" for warranty/support.
GDB reads only enough symbol data to know where to find the rest when needed; as
a result, the first prompt comes up very quickly.
1.2 Setting Display width
We now tell GDB to use a narrower display width than usual, so that examples fit in
this manual.
((gdb)) set width 70
We need to see how the m4 built-in changequote works. Having looked at the
source, we know the relevant subroutine is m4_changequote, so we set a breakpoint
there with the GDB break command.
1.3 Setting Breakpoints
Here we describe how to set a breakpoint.
((gdb)) break m4 changequote
Breakpoint 1 at 0x62f4: file builtin.c, line 879.
1.4 Running the executable under GDB
Using the run command, we start m4 under GDB control. As long as the control does
not reach the m4_changequote subroutine, the program runs as usual.
((gdb)) run
Starting program: /work/Editorial/gdb/gnu/m4/m4
define(foo,0000)
foo
0000
To trigger the breakpoint, we call changequote. GDB suspends execution of m4,
displaying information about the context where it stops.
changequote(<QUOTE>,<UNQUOTE>)
Breakpoint 1, m4_changequote (argc=3, argv=0x33c70)
at builtin.c:879
879 if (bad_argc(TOKEN_DATA_TEXT(argv[0]),argc,1,3))
1.5 Stepping to the next line in the source program
Now we use the command n (next) to advance execution to the next line of the current
function.
20A Sample GDB Session
((gdb)) n
882 set_quotes((argc >= 2) ? TOKEN_DATA_TEXT(argv[1])\
: nil,
1.6 Stepping into a subroutine
The set_quotes looks like a promising subroutine. We can go into it by using the
command s (step) instead of next. step goes to the next line to be executed in any
subroutine, so it steps into set_quotes.
((gdb)) s
set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>")
at input.c:530
530 if (lquote != def_lquote)
1.7 Examining the Stack
The display that shows the subroutine where m4 is now suspended (and its arguments)
is called a stack frame display. It shows a summary of the stack. We can use the
backtrace command (which can also be spelled bt), to see where we are in the stack
as a whole: the backtrace command displays a stack frame for each active subroutine.
((gdb)) bt
#0 set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>")
at input.c:530
#1 0x6344 in m4_changequote (argc=3, argv=0x33c70)
at builtin.c:882
#2 0x8174 in expand_macro (sym=0x33320) at macro.c:242
#3 0x7a88 in expand_token (obs=0x0, t=209696, td=0xf7fffa30)
at macro.c:71
#4 0x79dc in expand_input () at macro.c:40
#5 0x2930 in main (argc=0, argv=0xf7fffb20) at m4.c:195
We step through a few more lines to see what happens. The first two times, we can use
's'; the next two times we use n to avoid falling into the xstrdup subroutine.
((gdb)) s
0x3b5c 532 if (rquote != def_rquote)
((gdb)) s
0x3b80 535 lquote = (lq == nil || *lq == '\0') ? \
def_lquote : xstrdup(lq);
((gdb)) n
536 rquote = (rq == nil || *rq == '\0') ? def_rquote\
: xstrdup(rq);
((gdb)) n
538 len_lquote = strlen(rquote);
1.8 Printing Variable Values
The last line displayed looks a little odd in the listing above; we can examine the
variables lquote and rquote to see if they are in fact the new left and right quotes
we specified. We use the command p (print) to view their values.
1.6 Stepping into a subroutine21
((gdb)) p lquote
$1 = 0x35d40 "<QUOTE>"
((gdb)) p rquote
$2 = 0x35d50 "<UNQUOTE>"
1.9 Listing Source Code
lquote and rquote are indeed the new left and right quotes. To look at some context,
we can display ten lines of source surrounding the current line with the l (list)
command.
Let usstep past the two lines that set len_lquote and len_rquote, and then examine
the values of those variables.
((gdb)) n
539 len_rquote = strlen(lquote);
((gdb)) n
540 }
((gdb)) p len_lquote
$3 = 9
((gdb)) p len_rquote
$4 = 7
1.10 Setting Variable Values During a Session
That certainly looks wrong, assuming len_lquote and len_rquote are meant to be
the lengths of lquote and rquote respectively. We can set them to better values using
the p command, since it can print the value of any expression―and that expression
can include subroutine calls and assignments.
((gdb)) p len_lquote=strlen(lquote)
$5 = 7
((gdb)) p len_rquote=strlen(rquote)
$6 = 9
Is that enough to fix the problem of using the new quotes with the m4 built-in
defn? We can allow m4 to continue executing with the c (continue) command, and
then try the example that caused trouble initially:
22A Sample GDB Session
((gdb)) c
Continuing.
define(baz,defn(<QUOTE>foo<UNQUOTE>))
baz
0000
Success! The new quotes now work just as well as the default ones. The problem seems
to have been just the two typos defining the wrong lengths. We allow m4 to exit by
giving it an EOF as input:
C-d
Program exited normally.
The message`Program exited normally.' is from GDB;it indicates m4 has finished
executing. We can end our GDB session with the GDB quit command.
((gdb)) quit
1.10 Setting Variable Values During a Session23
24
2 Getting In and Out of GDB
This chapter discusses how to start GDB, and exit out of it. The essentials are:
•type '(gdb)' to start GDB.
•type quit or C-d to exit.
2.1 Invoking GDB
Invoke GDB by running the program (gdb). Once started, GDB reads commands from
the terminal until you tell it to exit.
You can also run (gdb) with a variety of arguments and options, to specify more of
your debugging environment at the outset.
The command-line options described here are designed to cover a variety of situations;
in some environments, some of these options may effectively be unavailable.
The most usual way to start GDB is with one argument, specifying an executable
program:
(gdb) program
You can also start with both an executable program and a core file specified:
(gdb) program core
You can, instead, specify a process ID as a second argument, if you want to debug a
running process:
(gdb) program 1234
would attach GDB to process 1234 (unless you also have a file named '1234'; GDB
does check for a core file first).
Taking advantage of the second command-line argument requires a fairly complete
operating system; when you use GDB as a remote debugger attached to a bare board,
there may not be any notion of “process”, and there is often no way to get a core dump.
GDB will warn you if it is unable to attach or to read core dumps.
You can run (gdb) without printing the front material, which describes GDB's
non-warranty, by specifying -silent:
gdb -silent
You can further control how GDB starts up by using command-line options. GDB itself
can remind you of the options available.
Type
(gdb) -help
to display all available options and briefly describe their use ('(gdb)-h' is a shorter
equivalent).
2.1 Invoking GDB25
All options and command-line arguments you give are processed in sequential order.
The order makes a difference when the `-x' option is used.
2.1.1 Choosing files
When GDB starts, it reads any arguments other than options as specifying an executable
file and core file (or process ID). This is the same as if the arguments were specified by
the '-se' and '-c' options respectively. (GDB reads the first argument that does not
have an associated option flag as equivalent to the '-se' option followed by that
argument; and the second argument that does not have an associated option flag, if
any, as equivalent to the '-c' option followed by that argument.)
If GDB has not been configured to included core file support, such as for most embedded
targets, then it will complain about a second argument and ignore it.
Many options have both long and short forms; both are shown in the following list.
GDB also recognizes the long forms if you truncate them, so long as enough of the
option is present to be unambiguous. (If you prefer, you can flag option arguments
with `--' rather than `-', though we illustrate the more usual convention.)
-symbols file
-s file
-exec file
-e file
-se file
Read symbol table from file file.
Use file file as the executable file to execute when
appropriate, and for examining pure data in
conjunction with a core dump.
Read symbol table from file file and use it as the
executable file.
-core file
-c file
-c numberConnect to process ID number, as with the attach
-command file
-x file
-directory directory
-d directory
26Getting In and Out of GDB
Use file file as a core dump to examine.
command (unless there is a file in core-dump format
named number, in which case `-c' specifies that file as
a core dump to read).
Execute GDB commands from file file. See “Command
files” (page 289).
Add directory to the path to search for source files.
-m, -mapped
Warning: this option depends on operating system facilities
that are not supported on all systems.
If memory-mapped files are available on your system
through the mmap system call, you can use this option
to have GDB write the symbols from your program
into a reusable file in the current directory. If the
program you are debugging is called '/tmp/fred', the
mapped symbol file is '/tmp/fred.syms'. Future GDB
debugging sessions notice the presence of this file, and
can quickly map in symbol information from it, rather
than reading the symbol table from the executable
program.
The '.syms' file is specific to the host machine where
GDB is run. It holds an exact image of the internal GDB
symbol table. It cannot be shared across multiple host
platforms.
-r, -readnow
You typically combine the -mapped and -readnow options in order to build a '.syms'
file that contains complete symbol information. (See “Commands to specify files”
(page 125), for information on '.syms' files.) A simple GDB invocation to do nothing
but build a '.syms' file for future use is:
gdb -batch -nx -mapped -readnow programname
2.1.2 Choosing modes
You can run GDB in various alternative modes―for example, in batch mode or quiet
mode.
-nx, -n
-quiet, -silent, -q
Read each symbol file's entire symbol table
immediately, rather than the default, which is to read
it incrementally as it is needed. This makes startup
slower, but makes future operations faster.
Do not execute commands found in any initialization
files (normally called '.gdbinit', or 'gdb.ini' on PCs).
Normally, GDB executes the commands in these files
after all thecommand optionsand argumentshave been
processed. See “Command files” (page 289).
“Quiet”. Do not print the introductory and copyright
messages. These messages are also suppressed in batch
mode.
-batchRun in batch mode. Exit with status 0 after processing
all the command files specified with '-x' (and all
commands from initialization files, if not inhibited with
2.1 Invoking GDB27
'-n'). Exit with nonzero status if an error occurs in
executing the GDB commands in the command files.
Batch mode may be useful for running GDB as a filter,
for example to download and run a program on another
computer; inorder to make this more useful, the message
Program exited normally.
(which isordinarily issued whenever a program running
under GDB control terminates) is not issued when
running in batch mode.
-nowindows, -nw
“No windows”. If GDB comes with a graphical user
interface (GUI) built in, then this option tells GDB to
only use the command-line interface. If no GUI is
available, this option has no effect.
-windows, -w
If GDB includes a GUI, then this option requires it to be
used if possible.
-cd directory
Run GDB using directory as its working directory, instead
of the current directory.
-dbxSupport additional dbx commands, including:
•use
•status (in dbx mode, status has a different
meaning than in default GDB mode.)
•whereis
•func
•file
•assign
•call
•stop
-fullname, -fGNU Emacs sets this option when it runs GDB as a
subprocess. It tells GDB to output the full file name and
line number in a standard, recognizable fashion each
time a stack frame is displayed (which includes each
time your program stops). This recognizable formatlooks
like two `\032' characters, followed by the file name,
line number, and character position separated by colons,
and a newline. The Emacs-to-GDB interface program
uses the two '\032' characters as a signal to display the
source code for the frame.
-epoch
The Epoch Emacs-GDB interface sets this option when
it runs GDB as a subprocess. It tells GDB to modify its
28Getting In and Out of GDB
-annotate level
-async
print routines so as to allow Epoch to display values of
expressions in a separate window.
This option sets the annotation level inside GDB. Its effect
is identicalto using `set annotate level' (see “GDB
Annotations” (page297)). Annotation level controls how
much information does GDB print together with its
prompt, values of expressions, source lines, and other
types of output. Level 0 is the normal, level 1 is for use
when GDB is run as a subprocess of GNU Emacs, level 2
is the maximum annotation suitable for programs that
control GDB.
Use the asynchronous event loop for the command-line
interface. GDB processes all events, such as user
1
keyboard input, via a special event loop. This allows
GDB to accept and process user commands in parallel
with the debugged process being run1, so you do not
need to wait for control to return to GDB before you type
the next command.
NOTE:As of version 5.0, the target side of the
asynchronous operation is not yet in place, so '-async'
does not work fully yet.
When the standard input is connected to a terminal
device, GDB uses the asynchronous event loop by
default, unless disabled by the '-noasync' option.
-noasync
Disable the asynchronous event loop for the
command-line interface.
-baud bps, -b bps
Set the line speed (baud rate or bits per second) of any
serial interface used by GDB for remote debugging.
-tty device, -t device
Run using device for your program's standard input and
output.
-tui
Use a Terminal User Interface. For information, use your
Web browser to read the file 'tui.html', which is
usually installed in the directory /opt/langtools/wdb/doc on HP-UX systems. Do not use this option if
you run GDB from Emacs (see “Using GDB under gnu
Emacs” (page 293)).
-xdb
Run in XDB compatibility mode, allowing the use of
certain XDB commands. For information, see the file
1. GDB built with DJGPP tools for MS-DOS/MS-Windows supports this mode of operation, but the event
loop is suspended when the debug target runs.
2.1 Invoking GDB29
-interpreter interp
-write
-statistics
-version
-pid
-inline
-src_no_g
'xdb_trans.html', which is usually installed in the
directory /opt/langtools/wdb/doc on HP-UX
systems.
Use the interpreter interp for interface with the
controlling program or device. This option is meant to
be set by programs which communicate with GDB using
it as a back end. For example, '--interpreter=mi'
causes GDB to use the gdbmi interface (see “The GDB/MI
Interface” (page 307)).
Open the executable and core files for both reading and
writing. This is equivalent to the 'set write on'
command inside GDB (see “Patching programs”
(page 122)).
This option causes GDB to print statistics about time and
memory usage after it completes each command and
returns to the prompt.
This option causes GDB to print its version number and
no-warranty blurb, and exit.
This option causes GDB to attach to a running process.
This option causes the debugger to start with the inline
debugging on.
This option is used to set the limited source level
debugging without compiling.
2.1.3 Redirecting WDB input and output to a file
To redirect WDB input and output to a file, use either of these commands to start the
debugger:
$ script log1
$ gdb
or
$ gdb | tee log1
2.2 Quitting GDB
quit [expression], qTo exit GDB, use the quit command (abbreviated q), or
type an end-of-file character (usually C-d). If you do not
supply expression, GDB will terminate normally; otherwise
it will terminate using the result of expression as the error
code.
An interrupt (often C-c) does not exit from GDB, but rather terminates the action of
any GDB command that is in progress and returns to GDB command level. It is safe to
30Getting In and Out of GDB
Loading...
+ 339 hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.