The Gnu source-level DeBugger
GDB allows you to see what is going on inside of a program while
it is executing. According to the man page, GDB allows you to do four
basic things:
- Start your program, specifying anything that might affect its behavior,
such as command line arguments.
- Make your program stop on specified conditions or at specified points
in the code.
- Examine what has happened and the state of the program at the point
you stop it.
- Change things in your program such as variable values or other memory
contents.
Starting GDB
To begin a gdb session from a shell, type:
% gdb program
or while in gdb type:
(gdb) file program
Entering Commands
You enter commands at the gdb prompt:
(gdb) _
GDB offers a history like tcsh or bash. To see previous commands press
the up arrow. GDB also offers tab completion. This allows you to type
in the first few characters of a command or symbol and then press tab.
GDB will fill in the missing letters or list all the possible matches.
Quitting GDB
To quit GDB, type:
(gdb) quit
or
Control-D
Running Programs
To effectively run GDB on a program, it must have been compiled with
debugging information. To do this, use the '-g' when compiling. If you
do not use this option, you will only see the assembly code, not the actual
C code.
To run the program after it has been read in by GDB type:
(gdb) r args
where args are any command line arguments that you wish to pass
to the program.
Setting Breakpoints
A breakpoint cause the execution of your program to stop when that breakpoint
has been reached. In its most basic form, a break point can be at a function
or at a line number.
To specify a simple breakpoint type:
(gdb) b linenum
-OR-
(gdb) b filename:linenum
-OR-
(gdb) b function
-OR-
(gdb) b filename:function
filename allows you to specify a breakpoint in a file containing
code that is not currently running. It is optional; without it the breakpoint
is set in the file that contains the code currently running. You can also
specify a breakpoint where execution will stop at that breakpoint if a
certain condition is met.
To do this type:
(gdb) b breakpoint if cond
where cond is a C boolean expression. The expression is evaluated
each time the breakpoint is reached. Execution stops when the expression
is true.
To see what breakpoints have been set, type:
(gdb) i breakpoints
To remove a breakpoint, type:
(gdb) d bnum
bnum can be obtained from "i breakpoints".
Continuing and Stepping Through the Program
To resume execution until your program encounters a signal, a breakpoint,
or finishes successfully, type:
(gdb) c
To execute a single line of code (i.e. step through the code), stepping
into functions, type:
(gdb) s
To execute a single line of code, stepping over functions, type:
(gdb) n
Basically, n continues execution of the code until the next source
line within the current stack frame is encountered. In other words, it steps
through the code that is listed in a given file only, source line by line.
s will step through the code and will enter function code when a
function is called. This may require switching between several different
files.
To continue until a function is finished executing, type:
(gdb) f
Examining Data
The usual way to examine data in your program is to type:
(gdb) p/f exp
where f is an optional format letter. If it is left out, the format
will will be appropriate for the data's data type. It can be one of the
following letters:
- x
- Regard the bits of the value as an integer, and print the integer
in hexadecimal.
- d
- Print as integer in signed decimal.
- u
- Print as integer in unsigned decimal.
- o
- Print as integer in octal.
- t
- Print as integer in binary. The letter `t' stands for "two".
- a
- Print as an address, both absolute in hexadecimal and as an offset
from the nearest preceding symbol. You can use this format used to discover
where (in what function) an unknown address is located:
(gdb) p/a 0x54320
$3 = 0x54320 <_initialize_vx+396>
- c
- Regard as an integer and print it as a character constant.
- f
- Regard the bits of the value as a floating point number and print
using typical floating point syntax.
p will evaluate and print the value of exp, which is a C expression.
exp is often just a variable name.
To have a value displayed each time GDB stops (to monitor a value as
you step through a program, for example), type:
(gdb) d/f exp
To set what is being displayed, type:
(gdb) i display
To remove one of these displayed values, type:
(gdb) und dnum
dnum can be obtained with "i display".
This has been a very basic introduction to GDB. GDB is an extremely
powerful tool. It also allows for signal handling, multiple thread handling,
other kinds of breakpoints, watchpoints, the ability to examine and the
stack, the symbol table, and registers, and many other things.
For more information pick up a reference handout at the CETS office
in 164 Moore. GDB also has internal help available by typing "h".
|