Docsity

Debugging and gdb, Study notes of Software Engineering

Toggle sidebar
bg1
Lecture 25: Debugging and gdb
Kenneth M. Anderson
Software Methods and Tools
CSCI 3308 - Fall Semester, 2004
November 15, 2004 © University of Colorado, 2004 2
Today’s Lecture
Debugging
The concept
Terminology
Types of Debuggers
gdb - The GNU Debugger
November 15, 2004 © University of Colorado, 2004 3
Debugging
Debugging is the process of finding faults in a
program after a failure has been detected
Typically
a program fails a test case (or a bug is reported)
a programmer is given the test case, checks out
the associated source code, and “debugs” the
program until the fault has been corrected
the program, having been fixed, passes the test
case and no longer exhibits the failure
November 15, 2004 © University of Colorado, 2004 4
Finding Faults
The hard part of debugging is locating faults
Once a fault is located, it is typically
straightforward to fix
Having a failing test case is helpful
because the fault must be in some part of the code
that was executed by the program before the
failure occurred
The entire program does not have to be examined
pf3
pf4
pf5

Partial preview of the text

Download Debugging and gdb and more Study notes Software Engineering in PDF only on Docsity!

Lecture 25: Debugging and

gdb

Kenneth M. Anderson

Software Methods and Tools

CSCI 3308 - Fall Semester, 2004

November 15, 2004 © University of Colorado, 2004

Today’s Lecture^ 

Debugging^ 

The concept

^

Terminology

^

Types of Debuggers

gdb - The GNU Debugger

November 15, 2004 © University of Colorado, 2004

Debugging^ 

Debugging is the process of finding faults in aprogram after a failure has been detected

Typically^ 

a program fails a test case (or a bug is reported)

^

a programmer is given the test case, checks outthe associated source code, and “debugs” theprogram until the fault has been corrected

^

the program, having been fixed, passes the testcase and no longer exhibits the failure

November 15, 2004 © University of Colorado, 2004

Finding Faults^ 

The hard part of debugging is locating faults^ 

Once a fault is located, it is typicallystraightforward to fix

Having a failing test case is helpful^ 

because the fault must be in some part of the codethat was executed by the program before thefailure occurred

^

The entire program does not have to be examined

© University of Colorado, 2004

Bridging the Gap^ 

One problem with faults is that they are notnecessarily located “near” their associated failure

^

Therefore, “bring the failure close to the fault”^ 

the idea being that if we do, less code needs to be examinedto find the fault ^ Thus, we often insert “print” statements into code to force thefailure to appear as soon as possible after the fault ^ Taken to the extreme, a programmer should be able to seethe value of any variable at any time during a program’sexecution November 15, 2004 © University of Colorado, 2004

Debugging Tools^ 

This is the purpose of debugging tools^ 

called “debuggers”

A debugger allows a programmer to monitorthe internal state of a program while itsexecuting

Two types of debuggers^ 

Interpretive

^

Direct Execution

© University of Colorado, 2004

Types of Debuggers^ 

Interpretive debugger^ 

works by reading a program and simulating itsexecution one line at a time

Direct Execution Debugger^ 

works by running the actual program in a specialmode where the debugger can read and write theprogram’s memory

November 15, 2004 © University of Colorado, 2004

Two styles of use^ 

Line-at-a-time^ 

A programmer loads a program into the debugger and “steps”through the program one line at a time. ^ The debugger stops the program after each line and givesthe programmer a chance to check the program’s variablesto see if it is operating correctly ^

Breakpoints^ 

A programmer loads a program into the debugger andspecifies “breakpoints” at various locations in the program ^ The program runs until it hits a breakpoint

© University of Colorado, 2004

The GNU Debugger^ 

The GNU Debugger (gdb) is a directexecution debugger^ 

There are separate processes for thedebugger and the program

^

All of the variables are kept in theprogram’s process

^

GDB can read/write the program’s memory

November 15, 2004 © University of Colorado, 2004

GDB Architecture

GNU Debugger

Program

Symbol Table

CodeData

© University of Colorado, 2004

Preparing to use the Debugger^ 

In order for gdb to process a program,it must be compiled using the “-g” flag

gcc -c -g program.cgcc program.o -o program



This flag tells the compiler to include asymbol table into the compiled program

November 15, 2004 © University of Colorado, 2004

What’s a symbol table?^ 

A symbol table records the mapping betweena program’s variables and their locations inmemory^ 

Executable code uses machine addresses toreference memory. The variable “x” might bestored at address 0xF8E

^

So rather than typing “print 0xF8E3” to see thevalue of the variable “x”, you can instead type“print x” and gdb uses the symbol table to locateand print the correct memory location

© University of Colorado, 2004

An Example

GNU Debugger“(gdb) print x”

Program x: 0xF8E

Code

November 15, 2004 © University of Colorado, 2004

More on the Symbol Table^ 

A symbol table is not automatically included in aprogram because^ 

it takes up space ^ its not required for a program to execute ^

You can still use gdb on a program that doesn’t havea symbol table^ 

however, you then have to type things like “print 0xF8E3”since the debugger will not be able to map these values tovariable names © University of Colorado, 2004

Debugging Programs^ 

A debugger does not automatically execute theprogram to be debugged^ 

You need to have a chance to configure a program’s values,set breakpoints, etc. before running it ^

Instead, it loads the program into a separate processand prints a prompt^ gdb programGNU gdb 4.17…(gdb)

November 15, 2004 © University of Colorado, 2004

Running programs in GDB^ 

Typing “run” at the prompt will run the program asnormal^ 

e.g. it will function as if you had invoked it from the shell ^

In order to debug the program, you need to set abreakpoint^ 

(gdb) break main ^ Breakpoint 1 at 0x229c ^ (gdb) run ^ Starting program: program ^ Breakpoint 1, 0x229c in main () ^ (gdb)

November 15, 2004 © University of Colorado, 2004

Example^ main() {

int i;i = 2;func(4); } func (int i) { … } ^ (gdb) backtrace ^ #0 func (i=4) at main.c: ^ #1 0x22b0 in main() at main.c: ^ (gdb) print i ^ 4 ^ (gdb) frame 1 ^ #1 0x22b0 in main () at main.c: ^ (gdb) print i ^ 2 November 15, 2004 © University of Colorado, 2004

Common Commands^ 

run - execute program ^ b or - break at line number or subroutine ^ list - list source code^  list filename:number ^ step - execute one line (step into subs) ^ continue - continue execution until next breakpoint or end ofprogram ^ next - execute next instruction (step over subs) ^ bt - view call stack ^ frame - select frame on call stack ^ print or