[ <] | [ >] | [ <<] | [ 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 |
gdb program core |
gdb program 1234 |
gdb --args gcc -O2 -c foo.c |
gdb -silent |
gdb -help |
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
Read symbol table from file file.
-exec file
-e file
Use file file as the executable file to execute when appropriate, and for examining pure data in conjunction with a core dump.
-se file
Read symbol table from file file and use it as the executable file.
-core file
-c file
Use file file as a core dump to examine.
-pid number
-p number
Connect to process ID number, as with the attach
command.
-command file
-x file
Execute GDB commands from file file. See section Command files.
-eval-command command
-ex command
Execute a single GDB 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 |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
You can run GDB in various alternative modes--for example, in batch mode or quiet mode.
-nx
-n
Do not execute commands found in any initialization files. Normally, GDB executes the commands in these files after all the command options and arguments have been processed. See section Command Files.
-quiet
-silent
-q
"Quiet". Do not print the introductory and copyright messages. These messages are also suppressed in batch mode.
-batch
Run 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 `-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. |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Here's the description of what GDB does during session startup:
Reads the init file (if any) in your home directory
(1) and executes all the commands in that file.
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