[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This chapter discusses how to start GDB, and how to get out of it. The essentials are:
2.1 Invoking GDB How to start GDB 2.2 Quitting GDB How to quit GDB 2.3 Shell Commands How to use shell commands inside GDB 2.4 Logging Output How to log GDB's output to a file
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 optionally have gdb
pass any arguments after the
executable file to the inferior using --args
. This option stops
option processing.
gdb --args gcc -O2 -c foo.c |
gdb
to debug gcc
, and to set
gcc
's command-line arguments (see section 4.3 Your Program's Arguments) to `-O2 -c foo.c'.
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.
2.1.1 Choosing Files Choosing files 2.1.2 Choosing Modes Choosing modes 2.1.3 What GDB Does During Startup What GDB does during startup
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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' (or `-p') 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'/`-p' option followed by that argument.) If the second argument begins with a decimal digit, GDB will first attempt to attach to it as a process, and if that fails, attempt to open it as a corefile. If you have a corefile whose name begins with a digit, you can prevent GDB from treating it as a pid by prefixing it with `./', e.g. `./12345'.
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
-pid number
-p number
attach
command.
-command file
-x file
-eval-command command
-ex command
This option may be used multiple times to call multiple commands. It may also be interleaved with `-command' as required.
gdb -ex 'target sim' -ex 'load' \ -x setbreakpoints -ex 'run' a.out |
-directory directory
-d directory
-r
-readnow
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
-batch-silent
stdout
is prevented (stderr
is
unaffected). This is much quieter than `-silent' and would be useless
for an interactive session.
This is particularly useful when using targets that give `Loading section' messages, for example.
Note that targets that give their output via GDB, as opposed to
writing directly to stdout
, will also be made silent.
-return-child-result
This option is useful in conjunction with `-batch' or `-batch-silent', when GDB is being used as a remote program loader or simulator interface.
-nowindows
-nw
-windows
-w
-cd directory
-fullname
-f
-epoch
-annotate level
The annotation mechanism has largely been superseded by GDB/MI (see section 24. The GDB/MI Interface).
--args
-baud bps
-b bps
-l timeout
-tty device
-t device
-tui
-interpreter interp
`--interpreter=mi' (or `--interpreter=mi2') causes GDB to use the GDB/MI interface (see section The GDB/MI Interface) included since GDB version 6.0. The previous GDB/MI interface, included in GDB version 5.3 and selected with `--interpreter=mi1', is deprecated. Earlier GDB/MI interfaces are no longer supported.
-write
-statistics
-version
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here's the description of what GDB does during session startup:
Init files use the same syntax as command files (see section 20.3 Command Files) and are processed by GDB in the same way. The init file in your home directory can set options (such as `set complaints') that affect subsequent processing of command line options and operands. Init files are not executed if you use the `-nx' option (see section Choosing Modes).
The GDB init files are normally called `.gdbinit'. The DJGPP port of GDB uses the name `gdb.ini', due to the limitations of file names imposed by DOS filesystems. The Windows ports of GDB use the standard name, but if they find a `gdb.ini' file, they warn you about that and suggest to rename the file to the standard name.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
quit [expression]
q
quit
command (abbreviated
q
), or type an end-of-file character (usually Ctrl-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 Ctrl-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).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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'.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You may want to save the output of GDB commands to a file. There are several commands to control GDB's logging.
set logging on
set logging off
set logging file file
set logging overwrite [on|off]
overwrite
if
you want set logging on
to overwrite the logfile instead.
set logging redirect [on|off]
redirect
if you want output to go only to the log file.
show logging
[ << ] | [ >> ] | [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