[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2. Getting In and Out of GDB

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] [ ? ]

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 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
This will cause 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.


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] [ ? ]

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' (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.

-c number
-pid number
-p number
Connect to process ID number, as with the attach command. If there is no such process, GDB will attempt to open a core file named number.

-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

-directory directory
-d directory
Add directory to the path to search for source and script files.

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.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.1.2 Choosing Modes

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 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.

Run in batch mode exactly like `-batch', but totally silently. All GDB output to 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.

The return code from GDB will be the return code from the child process (the process being debugged), with the following exceptions:

This option is useful in conjunction with `-batch' or `-batch-silent', when GDB is being used as a remote program loader or simulator interface.

"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.

-cd directory
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.

-annotate level
This option sets the annotation level inside GDB. Its effect is identical to using `set annotate level' (see section 25. 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 24. 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.

-baud bps
-b bps
Set the line speed (baud rate or bits per second) of any serial interface used by GDB for remote debugging.

-l timeout
Set the timeout (in seconds) of any communication used by GDB for remote debugging.

-tty device
-t device
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).

-interpreter interp
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 14.6 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.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.1.3 What GDB Does During Startup

Here's the description of what GDB does during session startup:

  1. Sets up the command interpreter as specified by the command line (see section interpreter).

  2. Reads the init file (if any) in your home directory(1) and executes all the commands in that file.

  3. Processes command line options and operands.

  4. 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.

  5. Reads command files specified by the `-x' option. See section 20.3 Command Files, for more details about GDB command files.

  6. Reads the command history recorded in the history file. See section 19.3 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 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] [ ? ]

2.2 Quitting GDB

quit [expression]
To exit GDB, use the 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] [ ? ]

2.3 Shell Commands

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
Invoke a standard shell to execute command string. If it exists, the environment variable 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
Execute the make program with the specified arguments. This is equivalent to `shell make make-args'.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.4 Logging Output

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 overwrite if you want set logging on to overwrite the logfile instead.
set logging redirect [on|off]
By default, GDB output will go to both the terminal and the logfile. Set redirect if you want output to go only to the log file.
show logging
Show the current values of the logging settings.

[ << ] [ >> ]           [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 April, 15 2007 using texi2html