A basic gdb session

Starting and quitting gdb

When starting gdb(1), you can specify a program, an object file, or a process identification (ID) number. You can attach or load any of these from inside a gdb session, but it is usually easier to specify them on the command line, as shown in the following example:

gdb [name of program [name of core file]]

You can specify a process number instead of a core file, which allows you to attach to a running process. If you give a process ID number, gdb looks first for a file with that name. If it does not find the file, it attaches to the process. Using the process ID might yield an error message, but it should then attach you to the program.

On startup, gdb reads your home initialization file, $HOME/.gdbinit (if it exists). It then processes the command line and reads the initialization file .gdbinit in the current directory (if it exists). You can prevent gdb from reading the initialization files by giving the -n option.

When you start gdb, it displays a copyright message. You can skip the copyright message by starting gdb with the --quiet option. The following is an example of the gdb copyright message:

GNU gdb 4.16.1
Copyright 1997 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General
Public License, and you are
welcome to change it and/or distribute copies of it
under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show
warranty" for details.
This GDB was configured as "i386-pc-interix".
(gdb)

The default gdb command prompt is (gdb). From then on, it is assumed that you use the --quiet option.

To end, type quit at the (gdb) prompt. If a process is running, gdb asks if you really want to quit, as shown in the following example:

(gdb) quit
The program is running. Quit anyway (and kill it)?
(y or n)

You can quit by typing y and pressing ENTER. If no process is running inside the debugger, gdb exits directly.

Debugging a program

There are two basic approaches to debugging a program:

Basic gdb commands

The following commands are basic for using gdb:
Command Description
run [args] Runs the program with the arguments specified by args. If no arguments are specified, the last arguments given are used.
break [address] Sets a break point to stop execution at the specified address. The address can be a function name, a line in the current file, a file name and a line number (as in filename:number), or an offset from the current stop. If no address is given, the break point is set at the next instruction.
continue [count] Continues execution. If count is specified, ignores this break point for the next count passes.
step [count] Executes until the next line is reached; repeats count times.
next [count] Executes next line, including function calls; repeats count times.
list [address] Shows the next ten lines of source, or shows lines around address. The address can be [filename:]number, or an offset (indicated by + or -), or a range: first,last.
backtrace [n] Prints all frames in the stack. If n is given and is positive, prints n innermost frames. If n is given and is negative, prints n outermost frames.
print expression Prints the value of expression.
quit Exits gdb.

This section contains two sample programs, both of which are located in the $INTERIX_ROOT/usr/examples/gdb directory: