Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




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

4.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 (Refer to Section 6.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.

4.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 ./, 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.)

-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. Refer to Section 22.3 Command files.

-directory directory, -d directory

Add directory to the path to search for source files.

-m, -mapped

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

-r, -readnow

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 -mapped and -readnow options in order to build a .syms file that contains complete symbol information. (Refer to Section 17.1 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

4.1.2. Choosing modes

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. Refer to Section 22.3 Command files.

-quiet, -silent, -q

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

-nowindows, -nw

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

-windows, -w

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.

-fullname, -f

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 (refer to Chapter 27 gdb Annotations). Annotation level controls how much information does gdb print 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 2 is the maximum annotation suitable for programs that control gdb.


Use the asynchronous event loop for the command-line interface. gdb processes all events, such as user keyboard input, via a special event loop. This allows gdb to accept and process user commands in parallel with the debugged process being run[1], so you don't need to wait for control to return to gdb before you type the next command. (Note: as of version 5.1, the target side of the asynchronous operation is not yet in place, so -async does not work fully yet.)

When the standard input is connected to a terminal device, gdb uses the asynchronous event loop by default, unless disabled by the -noasync option.


Disable the asynchronous event loop for the command-line 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.

-tty device, -t device

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


Activate the Terminal User Interface when starting. The Terminal User Interface manages several text windows on the terminal, showing source, assembly, registers and gdb command outputs (refer to Chapter 24 gdb Text User Interface). Do not use this option if you run gdb from Emacs (refer to Chapter 25 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 (refer to Chapter 23 Command Interpreters.

-interpreter=mi (or -interpreter=mi2) causes gdb to use the current gdb/mi interface (refer to Chapter 26 The gdb/mi Interface). The previous gdb/mi interface, included in gdb version 5.3, can be selected with -interpreter=mi1. Earlier gdb/mi interfaces are not supported.


Open the executable and core files for both reading and writing. This is equivalent to the set write on command inside gdb (refer to Section 16.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.



gdb built with djgpp tools for MS-DOS/MS-Windows supports this mode of operation, but the event loop is suspended when the debuggee runs.

  Published under the terms of the GNU General Public License Design by Interspire