Princeton University
COS 217:  Introduction to Programming System

Precept 4:  The gdb Debugger

Purpose

Help you learn how to use gdb to debug C programs

Reading

Loukides & Oram, Chapter 6

Overview of gdb

Powerful debugger

Far more convenient than using temporary calls to printf

Allows you to:

Set breakpoints

Step through program one line at a time

Examine values of variables at various points during program execution

Examine stack

Etc.

[Refer to The gdb Debugger for C Programs Summary Sheet]

Setup

mystring
    mystring.h
    mystring.c
    testmystring.c

Compiling for gdb

To prepare to use gdb, must compile with -g option

gcc -g -o testmystring testmystring.c mystring.c

Places debugging information in .o files, and thus in executable binary file

Running gdb

Can run from shell prompt (not shown)

Can run within Emacs (preferred)

Examples:

xemacs
ESC X gdb testmystring

Running the Program

run: Run the program (with specified command-line arguments)

run

set args: Set the program's command-line arguments

show args: Show the program's command-line arguments

Redirection works, but may need to specify absolute filename instead of relative filename

[Problem: How to simulate EOLN within Emacs?  Ctrl-D doesn't work]

Using Breakpoints

break: Set a breakpoint at specified function, or file:linenum

continue: Continue execution after breaking

kill: Stop execution

break main
break mystrlen
info breakpoints
run
continue
continue
kill

clear: Clear specified (or current) breakpoint

clear mystrlen
info breakpoints
run
continue

delete: Delete specified (or all) breakpoint by number

disable: Disable specified (or all) breakpoint by number -- breakpoint still exists. but is inactive

disable
info breakpoints
run

enable: Enable specified (or all) breakpoint by number

enable
info breakpoints
run
continue

Others:  condition, commands

Stepping through the Program

next: Step over next line

step: Step into next line

finish: Step out of current function

run
step (into testmystrlen)
next (several times, passing over calls to mystrlen)
step (into mystrlen)
next (several times)
finish
continue

Examining Variables

print: Print the value of the specified expression

run
step (into testmystrlen)
step (into mystrlen)
next (several times, until after calls to Stack_push)
print pc
print *pc

whatis: Print type of specified variable

whatis pc
whatis *pc

ptype: Print definition of specified type

(Useful with structures)

continue

display, info display, undisplay: Specify an expression that should be printed at each breakpoint

printf: Print using C-style format string

Typically used only with commands command

Examining the Call Stack

where: Print the call stack

backtrace: Same as where

run
next (several times)
step (into testmystrlen)
step (into mystrlen)
where
continue

Especially important when analyzing a segmentation fault

frame: Print top of stack (not very useful)

up: Move the context toward bottom of stack

Allows printing of variables in calling function

down: Move the context toward top of stack

Undoes what up does

Working with Signals

(We'll study signals later in the course)

Command Abbreviations

Omitting command reissues previous one

Can scroll backward in Emacs to reissue command

Most commonly used gdb commands have one-letter abbreviations

Can abbreviate a command to a unique prefix

TAB key completes command

TAB key twice shows list of possibilities

Even variable names can be abbreviated!

 

 

Copyright ©2001 by Robert M. Dondero, Jr.