Go to the previous, next section.

Getting In and Out of GDB

This chapter discusses how to start GDB, and how to get out of it. (The essentials: type `gdb' to start GDB, and type quit or C-d to exit.)

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.

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.

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

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
Connect to process ID number, as with the 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
Execute GDB commands from file file. See section Command files.

-directory directory
-d directory
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 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 `./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.

The -mapped and -readnow options are typically combined 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

Choosing modes

You can run GDB in various alternative modes--for example, in batch mode or quiet mode.

Do not execute commands from any initialization files (normally called `.gdbinit'). Normally, the commands in these files are executed 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.

-cd directory
Run GDB using directory as its working directory, instead of the current directory.

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.

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

-tty device
Run using device for your program's standard input and output.

Quitting GDB

To exit GDB, use the quit command (abbreviated q), or type an end-of-file character (usually C-d).

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

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 the standard shell to execute command string. If it exists, the environment variable SHELL determines which shell to run. Otherwise GDB uses /bin/sh.

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

Go to the previous, next section.