Introduction to the GNU debugger gdb

Lab 6
CSCI 112

Note: No lab next week (10/6/05)


Goals:

Introduce students to the basics of gdb so that they can use it to find bugs in their class projects.


Lecture Notes:

You can learn about gdb using the man command
$ man gdb
Complete gdb documentation is at gdb manual.

Starting gdb
1) Compile your programs with the -g option.  All .o files and the final linking must have the -g option
2) at the command prompt type gdb followed by your executable name
$ g++ -g -o p1 p1.cc
$ gdb p1
GNU gdb 5.1.0.1
Copyright 2001 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 "sparc-sun-solaris2.8".
(gdb)
3) (gdb) is the prompt for gdb.  There are lots of commands that you can type here.  For example, if you type run, your program will start executing.

The following section provides an overview of the most commonly used commands
gdb commands (these are the most basic commands, this is NOT a complete list)

Command
Shortcut
Description
quit
q
qut the debugger and exit.  If a program is running it will ask you if you still want to quit.
help
h
W/o and argument lists help topics.  If followed by a command, will describe that command:  e.g. help run
run
r
Start running the current program.  May be followed with command line arguments:  e.g.   run < t01
print
p
Print the given variable.  For example, if there is a variable i in the current context, "print i" will print the value of i.
list
l
List the C++ code for the currently executing instruction.  List advances through the code each time it is called.
list 42
l 42
Lists the code at line 42 (you may pass any number here)
where
w
Prints the current line number and all the functions that were called to get there (prints the run time stack).
break
b
Set a breakpoint.  Must specify a line number for the curren file, or filename:line for another file
step
s
Execute one and only one line of code.
next
n
Same as step, but skips over function calls.
finish
f
Execute until the end of the current function.
continue
c
Continue execution until the next breakpoint or until the end of the program.



Exercise Setup:
I have created several sample programs for you to use with the debugger.

Copy these files into your own directory.  They are in ~tyson/112/src/lab6

    $ mkdir lab6
    $ cd lab6
    $ cp ~tyson/112/src/lab6/*  .

These files are also available on the web: src/lab6.

Once you have copied these files, cd to that directory and type make.

Exercise 1:
run gdb with the executable p1

    $ gdb p1

type run at the (gdb) to run this program

this program crashes and gdb tells you that it crashes on line 9

type list at the (gdb) to list the source code

look at the source code to see what the problem is

type quit at the (gdb) to quit the debugger

Exercise 2:
run gdb with the executable p2

    $ gdb p2

type run at the (gdb) to run this program

this program crashes just like p1

type where at the (gdb) to see the list of functions that were called
 
notice that the where command told you the arguments to the function f() and g()

type list to list the current source code

look at the source code to see what is happening

you can print out any variable from within gdb using the print command

type   print i   at the (gdb) to see the value of i  (if you dereference a NULL pointer, you get a segmentation fault)

Exercise 3:
run gdb with the executable p3

    $ gdb p3

type run at the (gdb) to run this program

Why does it crash?

Try printing the different variables:
(gdb) p cur
(gdb) p *cur

Here are some hints if you can't figure it out:

You can find the error by watching the execution of the insert function.

Insert a breakpoint at the first line of the insert function

    (gdb) break 25

now start the execution over again

    (gdb) run

it will ask you if you want to start from the beginning, answer yes

the execution will stop at line 25 (the breakpoint you set) when insert is called for the first time

use the where command to see the run time stack
    Linked_list::insert() was called from line 40
    its argument i = 1

use the list command to see the code

try printing the different variables using the print command

    hint:  the variable this points to the current object, try printing this and *this


Exercise 4:
Program p4 is the same program as p1 but is compiled w/o the -g option

run gdb with the executable p4

    $ gdb p4

run the program

notice that you don't get the same level of information that you got in Exercise 2

type typing where at the (gdb)