This chapter discusses how to start GDB, and how to get out of it. The essentials are:
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).
All options and command line arguments you give are processed in sequential order. The order makes a difference when the `-x' option is used.
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
-core file
-c file
-c number
attach
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).
-command file
-x file
-directory directory
-d directory
-m
-mapped
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 section Commands to specify files, 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
You can run GDB in various alternative modes--for example, in batch mode or quiet mode.
-nx
-n
-quiet
-silent
-q
-batch
0
after processing all the
command files specified with `-x' (and all commands from
initialization files, if not inhibited with `-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; in order to
make this more useful, the message
Program exited normally.(which is ordinarily issued whenever a program running under GDB control terminates) is not issued when running in batch mode.
-nowindows
-nw
-windows
-w
-cd directory
-fullname
-f
-epoch
-annotate level
-async
-noasync
-baud bps
-b bps
-tty device
-t device
-tui
-interpreter interp
-write
-statistics
-version
quit [expression]
q
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 type the interrupt character at any time because GDB does not allow it to take effect until a time when it is safe.
If you have been using GDB to control an attached process or
device, you can release it with the detach
command
(see section Debugging an already-running process).
If you need to execute occasional shell commands during your
debugging session, there is no need to leave or suspend GDB; you can
just use the shell
command.
shell command string
SHELL
determines which
shell to run. Otherwise GDB uses the default shell
(`/bin/sh' on Unix systems, `COMMAND.COM' on MS-DOS, etc.).
The utility make
is often needed in development environments.
You do not have to use the shell
command for this purpose in
GDB:
make make-args
make
program with the specified
arguments. This is equivalent to `shell make make-args'.
Go to the first, previous, next, last section, table of contents.