Title: GDB The GNU Project Debugger
1GDBThe GNU Project Debugger
- CS202
- Dec 7, 2006
- Semsak Buntha
- Office 205A
- Email buntha_at_cse.unr.edu
2Get ready before a ride
- Download
- www.cse.unr.edu/buntha/202/gdb/gdb.tgz
- Extract files
- tar xvfz gdb.tgz
- Enter gdb directory
- cd gdb
3Compilation for gdb
- g -g Wall -c file1.cpp
- g -g Wall -c file2.cpp
- g -o myout file1.o file2.o
or
- g -g -o myout file1.cpp file2.cpp
-g causes the compiler to generate an augmented
symbol table Without -g, we cannot use gdb
4Start Stop
- Start gdb
- gdb program core.xxxx
- e.g. gdb myout
- Stop gdb
- (gdb) quit
5Basic commands
- list
- break, info, delete
- run, continue where
- finish, next, step
- print display
- whatis ptype
6list
- (gdb) l
- - show 10 lines surrounding the code that has
just executed - - if the program has not run yet, it will show
at main(). - (gdb) l 5
- - show 10 lines start from 1 of current file
- (gdb) l div.cpp1, 13
- - show the lines between 1 and 15 of file
div.cpp
7break, info, delete
- break - set a breakpoint
- (gdb) b main.cpp 5
- (gdb) b div.cpp 6
- (gdb) b 8
- Info break - see all breakpoints
- (gdb) I b
- delete - delete a breakpoint
- (gdb) d 3
- note we can disable and enable a breakpoint
8Source code
div.cpp
main.cpp
1 include "div.h" 2 using namespace
std 3 4 template ltclass Tgt 5
PairltTgtPair(T xx, T yy) 6 x
xx 7 y yy 8 9
10 template ltclass Tgt 11 void
PairltTgtresult() 12 cout ltlt x/y
ltlt endl 13
1 include "div.h" 2 include
"div.cpp" 3 4 int main() 5
Pairltdoublegt a(18, 4) 6
Pairltintgt b(18, 4) 7 a.result()
8 b.result() 9 return
0 10
Existing breakpoints
Deleted breakpoint
9run, continue where
- run start running the program
- (gdb) r
- Note stop program with CTRL-C
- exit gdb with (gdb) q
- continue execute until next break point
- (gdb) c
- where list all functions called in hierarchy
- (gdb) where
- Note most recent first
10Source code
div.cpp
main.cpp
1 include "div.h" 2 using namespace
std 3 4 template ltclass Tgt 5
PairltTgtPair(T xx, T yy) 6 x
xx 7 y yy 8 9
10 template ltclass Tgt 11 void
PairltTgtresult() 12 cout ltlt x/y
ltlt endl 13
1 include "div.h" 2 include
"div.cpp" 3 4 int main() 5
Pairltdoublegt a(18, 4) 6
Pairltintgt b(18, 4) 7 a.result()
8 b.result() 9 return
0 10
First stop
Second stop after continue
11finish, next, step
- finish continues until the function return
- (gdb) finish
- Next executes the next line. Not stop inside
the function - (gdb) n
- step enters the function and executes one line
at a time - (gdb) s
12Source code
div.cpp
main.cpp
1 include "div.h" 2 using namespace
std 3 4 template ltclass Tgt 5
PairltTgtPair(T xx, T yy) 6 x
xx 7 y yy 8 9
10 template ltclass Tgt 11 void
PairltTgtresult() 12 cout ltlt x/y
ltlt endl 13
1 include "div.h" 2 include
"div.cpp" 3 4 int main() 5
Pairltdoublegt a(18, 4) 6
Pairltintgt b(18, 4) 7 a.result()
8 b.result() 9 return
0 10
Current stop
Next stop after command finish
13Source code
div.cpp
main.cpp
1 include "div.h" 2 using namespace
std 3 4 template ltclass Tgt 5
PairltTgtPair(T xx, T yy) 6 x
xx 7 y yy 8 9
10 template ltclass Tgt 11 void
PairltTgtresult() 12 cout ltlt x/y
ltlt endl 13
1 include "div.h" 2 include
"div.cpp" 3 4 int main() 5
Pairltdoublegt a(18, 4) 6
Pairltintgt b(18, 4) 7 a.result()
8 b.result() 9 return
0 10
Next command
Step command
14print display
- print show value of variable
- (gdb) print x
- (gdb) n
- (gdb) print a
- display show value whenever stops
- (gdb) display a
- (gdb) display b
- (gdb) n
- (gdb) undisplay 2
15whatis ptype
- whatis show datatype
- (gdb) whatis a.x
- (gdb) whatis b.x
- (gdb) whatis a
- ptype show structure
- (gdb) ptype a
- (gdb) ptype b
16Question?
- Office SEM 205A 9am-4pm
- Email buntha_at_cse.unr.edu