Getting In and Out of GDB
This chapter discusses how to start GDB, and how to get out of it. The essentials are:
- type `gdb' to start GDB.
- type quit or C-d to exit.
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:
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
gdb --args gcc -O2 -c foo.c
This will cause
gdb to debug
gcc, and to set
gcc's command-line arguments (see section 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
You can further control how GDB starts up by using command-line options. GDB itself can remind you of the options available.
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' (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 `./', eg. `./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.)
- 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.
- Use file file as a core dump to examine.
Connect to process ID number, as with the
attachcommand. If there is no such process, GDB will attempt to open a core file named number.
- Execute GDB commands from file file. See section Command files.
- Add directory to the path to search for source files.
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
mmapsystem 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.
- 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.
You typically combine the
-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.
- 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". Do not print the introductory and copyright messages. These messages are also suppressed in batch mode.
Run in batch mode. Exit with status
0after 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.
- "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.
- If GDB includes a GUI, then this option requires it to be used if possible.
- Run GDB using directory as its working directory, instead of the current directory.
- GNU 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 format looks 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.
- The Epoch Emacs-GDB interface sets this option when it runs GDB as a subprocess. It tells GDB to modify its 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 identical to using `set annotate level' (see section GDB Annotations). The annotation level controls how much information GDB prints 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 3 is the maximum annotation suitable for programs that control GDB, and level 2 has been deprecated. The annotation mechanism has largely been superseded by GDB/MI (see section The GDB/MI Interface).
- Change interpretation of command line so that arguments following the executable file are passed as command line arguments to the inferior. This option stops option processing.
- Set the line speed (baud rate or bits per second) of any serial interface used by GDB for remote debugging.
- Set the timeout (in seconds) of any communication used by GDB for remote debugging.
- Run using device for your program's standard input and output.
- Activate the Text User Interface when starting. The Text User Interface manages several text windows on the terminal, showing source, assembly, registers and GDB command outputs (see section GDB Text User Interface). Alternatively, the Text User Interface can be enabled by invoking the program `gdbtui'. Do not use this option if you run GDB from Emacs (see section Using GDB under GNU Emacs).
- 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. See section Command Interpreters. `--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.
- Open the executable and core files for both reading and writing. This is equivalent to the `set write on' command inside GDB (see section Patching programs).
- 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.
What GDB does during startup
Here's the description of what GDB does during session startup:
- Sets up the command interpreter as specified by the command line (see section Choosing modes).
- Reads the init file (if any) in your home directory(1) and executes all the commands in that file.
- Processes command line options and operands.
- Reads and executes the commands from init file (if any) in the current working directory. This is only done if the current directory is different from your home directory. Thus, you can have more than one init file, one generic in your home directory, and another, specific to the program you are debugging, in the directory where you invoke GDB.
- Reads command files specified by the `-x' option. See section Command files, for more details about GDB command files.
- Reads the command history recorded in the history file. See section Command history, for more details about the command history and the files where GDB records it.
Init files use the same syntax as command files (see section 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'. On some configurations of GDB, the init file is known by a different name (these are typically environments where a specialized form of GDB may need to coexist with other forms, hence a different name for the specialized version's init file). These are the environments with special init file names:
- 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.
- VxWorks (Wind River Systems real-time OS): `.vxgdbinit'
- OS68K (Enea Data Systems real-time OS): `.os68gdbinit'
- ES-1800 (Ericsson Telecom AB M68000 emulator): `.esgdbinit'
- CISCO 68k: `.cisco-gdbinit'
To exit GDB, use the
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
(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 string
Invoke a standard shell to execute command string.
If it exists, the environment variable
SHELLdetermines which shell to run. Otherwise GDB uses the default shell (`/bin/sh' on Unix systems, `COMMAND.COM' on MS-DOS, etc.).
make is often needed in development environments.
You do not have to use the
shell command for this purpose in
makeprogram with the specified arguments. This is equivalent to `shell make make-args'.
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
- Enable logging.
set logging off
- Disable logging.
set logging file file
- Change the name of the current logfile. The default logfile is `gdb.txt'.
set logging overwrite [on|off]
By default, GDB will append to the logfile. Set
overwriteif you want
set logging onto overwrite the logfile instead.
set logging redirect [on|off]
By default, GDB output will go to both the terminal and the logfile.
redirectif you want output to go only to the log file.
- Show the current values of the logging settings.
Go to the first, previous, next, last section, table of contents.