Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Timothy A. Gonsalves
TeNeT Group
Dept of Computer S
ien
e & Engineering
Indian Institute of Te
hnology, Madras - 600 036
E-mail: tagooty.TeNeT.res.in
July 1999
Contents
1 Introdu
tion
2 Getting Started
3 Program Control
4 Data A ess
5 Sour e Files
6 Core Dumps
7 Controlling gdb
8 Advan ed Topi s
Introdu tion
debugger allows you to
ontrol exe
ution of the program, suspending the exe
ution at
ertain lines. You
an then examine and even
hange the values of any
variables in the program. All this is done without tou
hing the sour
e
ode.
Indeed, the most powerful method of debugging is single-stepping line-by-line
through the
ode, examining the values of key variables after every statement.
It is only with a symboli
debugger that this is possible.
1.1
Typographi al Conventions
le
Exit gdb.
Getting Started
First,
ompile your program using the -g option. This instru
ts the
ompiler
to put information about the symbols { fun
tions and variables { into the exe
uteable le for use by the debugger. Next, invoke gdb with the name of the
program:
% g
-g -o myprog myprog.
% gdb myprog
gdb>
To exit from gdb, type quit. For a list of
ommands, type help.
In the se
tions below, we des
ribe some of the more useful gdb
ommands.
Although all
ommands are full words, you
an type any unambiguous prex.
For instan
e,
ont instead of
ontinue. You
an also dene short aliases for
frequently-used
ommands (see se
tion 7).
2.1
Getting Help
Use the help
ommand in gdb for a list of topi
s. Then type help topi
for
details on a spe
i
topi
. help
md gives help on the
ommand
md. For
omprehensive
overage of gdb, read the Info pages. At the shell prompt, type
info. When info starts, type mgdb<ENTER>). Alternatively, if you use
Ema
s, type the Ema
s
ommand <Ctrl-h i> to start Info.
Program Control
break nnn
break fun
run
ontinue
step
exe
ute the next sour
e line and then break. Steps into
fun
tion
alls.
next
exe
ute the next sour
e line and then break. Steps over
fun
tion
alls.
nish
wat h expr
<ENTER>
Before running the program, set some break-points. The run
ommand
optionally takes
ommand-line arguments and I/O redire
tion:
gdb> run -n *.
< infile
Data A ess
When gdb stops exe
ution at a break-point or after a next or step, you
an
examine and modify variables in the program.
print x
print *node
print F(a+b)
alls the fun tion F() with argument a+b and prints its return value
set x=10
You
an print the value of any arbitrary expression in the language of the
program being debugged. By
alling a fun
tion with a variety of arguments,
you
an test the fun
tion without having to write testing
ode.
Sour e Files
list nnn
sear h pat
sear
hes forward for the next sour
e line
ontaining the
regular-expression pat
reverse-sear h
The sear
h
ommands without an argument use the previous argument. Note
that a simple form of regular expression is a string. Example, sear
h MyFun
will sear
h for the next
all to MyFun
or its denition.
3
Core Dumps
When gdb starts, it tells you the line number in whi
h the ex
eption o
urred.
You
an use print to examine any variables. To see how the program got to
that line, use the
ommand bt (short for ba
ktra
e). This shows all fun
tion
alls from main() to the fun
tion in whi
h the fault o
urred.
Controlling gdb
dene md
show user
show
info
.gdbinit
a gdb ommand le
When gdb starts, it automati
ally reads /.gdbinit if it exists, then .gdbinit
in the
urrent dire
tory, if it exists. Thus, you
an
ustomise gdb to your liking, and also
ustomise it for ea
h proje
t (assumed to be in a separate subdire
tory). These
ommand les
an
ontain any gdb
ommands as you would
type them in gdb. Any line starting with a # is a
omment, and blank lines are
ignored.
Advan ed Topi s
This guide merely s
rat
hes the surfa
e of what is possible with gdb. Most of the
ommands des
ribed here have many more features. gdb also has many more
features su
h as allowing you to debug an already running pro
ess, to handle
signals, to step at the ma
hine instru
tion level. Use the help
ommand in gdb
and read the Info pages (see se
tion 2.1).