SlideShare a Scribd company logo
Debugging Techniques for C Programs
CS3411 Debugging 2
Debugging Basics
• Will focus on the gcc/gdb combination.
• Will also talk about the ddd gui for gdb (lots of value added to
gdb).
• First, debugging in the abstract:
– Program state is a snapshot of all variables, PC, etc.
– A statement in your program transforms one program state
into another.
– You should be able (at some level) to express what you
expect the state of your program to be after every statement.
– Often state predicates on program state; i.e., “if control is
here, I expect the following to be true.”
• Map into a toy example.
CS3411 Debugging 3
Small Example: ave.c
#
inc
lude <s
td
io
.h>
i
n
tsum=0, va
l
,num=0;
doub
le ave;
main(
)
{
whi
le(scanf
(
"%dn"
,&val
)!= EOF){
sum += va
l
;
num++;
}
i
f(num > 0){
ave = sum/num;
p
r
in
t
f
(
"Averageis %f

n"
, ave)
;
}
}
sum should be 0 and num
should be 0.
sum should be the total of
the num input values and
there is no more input.
sum should be the total of
the num input values
processed.
ave should be the floating
point mean of the num input
data values.
CS3411 Debugging 4
Small Example: ave.c
% a.out
1
Average i
s 1
.000000
% a.out
1
2
3
Average i
s 2
.000000
% a.out
1
2
3
4
Average i
s 2
.000000
Experienced programmer can probably “eyeball debug” the
program from this output
CS3411 Debugging 5
Using gdb
• Make sure to compile source with the -g switch asserted.
• In our case, gcc -gave.c
• Breakpoint: line in source code at which debugger will pause
execution. At breakpoint, can examine values of relevant
components of program state. break command sets a
breakpoint; c
l
ear removes the breakpoint.
• Diagnostic pr
in
t
f
(
)crude, but effective way of getting a snapshot
of program state at a given point.
• Once paused at a breakpoint, use gdb pr
in
t, or displ
ay to show
variable or expression values. disp
lay will automatically print
values when execution halts at breakpoint.
• From a breakpoint, may step or nextto single step the program.
s
tep stops after next source line is executed. nextsimilar, but
executes functions without stopping.
CS3411 Debugging 6
Using gdb
• May find out where execution is, in terms of function call chain,
with the where command; also shows function argument values.
• Apply some of this in context of bogus averaging program.
• To make things easier, put the problematic data set in a file
named data.
% a.ou
t < da
ta
Averagei
s 2
.000000
CS3411 Debugging 7
Using gdb (ave.c)
% gdb a.ou
t
GNU gdb 6
.1
Copyr
ight 2004 Free Software Foundat
ion,Inc
.
GDB isf
ree so
f
t
ware, covered by the GNU Genera
l Pub
l
i
c
L
icense, and you a
re we
l
come to change i
tand/or d
is
t
r
ibu
t
e
cop
ies ofi
tundercer
ta
in cond
i
t
ions.
Type "show copy
ing"tosee the cond
i
t
ions.
Thereis abso
lu
t
e
ly no war
ran
tyf
or GDB. Type "
show war
ran
ty
”
fo
r de
ta
i
l
s
.
This GDB was conf
igured as "
i586-suse-
l
inux"
.
.
.
Using host
l
i
b
th
read_dbl
ib
rary"
/
l
i
b
/
t
l
s
/
l
ib
th
read_db.so.1"
.
(gdb)
CS3411 Debugging 8
Using gdb (ave.c)
(gdb)l
1 #
incl
ude <s
td
i
o.h>
2
3 in
tsum=0, va
l
,num=0;
4 doub
le ave
;
5
6 ma
i
n(
)
7 {
8 whi
l
e(scan
f
(
"%dn"
,&va
l
)!= EOF) {
9 s
um += va
l
;
10 num++;
(gdb)l
11 }
12 i
f(num > 0){
13 ave = sum/num;
14 pr
in
t
f
(
"Averageis%fn"
,ave)
;
15 }
16 }
17
(gdb)
Interesting point: top of main loop.
Another interesting point: just
before ave is computed.
CS3411 Debugging 9
Using gdb (ave.c)
(gdb) b
reak 8
Breakpo
i
nt 1 a
t 0x80483dc:f
i
l
e ave
.c
,l
i
ne 8
.
(gdb) b
reak 13
Breakpo
i
nt 2 a
t 0x8048414:f
i
l
e ave
.c,l
i
ne 13
.
(gdb) d
isp
lay num
(gdb) d
isp
lay va
l
(gdb) d
isp
lay sum
(gdb)r <data
Star
t
ing program:/
home/
jmayo/courses
.d …
Breakpo
i
nt 1
, ma
in (
)a
tave
.
c
:8
8 whi
l
e(scanf
(
"%dn"
,&va
l
)!= EOF) {
3: sum = 0
2: va
l = 0
1: num = 0
(gdb) c
Cont
inu
ing.
Breakpo
i
nt 1
, ma
in (
)a
tave
.
c
:8
8 whi
l
e(scanf
(
"%dn"
,&va
l
)!= EOF) {
3: sum = 1
2: va
l = 1
1: num = 1
CS3411 Debugging 10
Using gdb (ave.c)
(gdb) c
Cont
inu
i
ng.
Breakpoi
n
t1
, ma
in(
)a
tave.c
:8
8 wh
i
le(scanf
(
"%dn"
,&va
l
)!
= EOF) {
3: sum = 3
2: va
l =2
1: num = 2
(gdb) c
Cont
inu
i
ng.
Breakpoi
n
t1
, ma
in(
)a
tave.c
:8
8 wh
i
le(scanf
(
"%dn"
,&va
l
)!
= EOF) {
3: sum = 6
2: va
l =3
1: num = 3
CS3411 Debugging 11
Using gdb (ave.c)
(gdb) c
Cont
inu
i
ng.
Breakpoi
n
t1
, ma
in(
)a
tave.c
:8
8 wh
i
le(scanf
(
"%dn"
,&va
l
)!
= EOF) {
3: sum = 10
2: va
l =4
1: num = 4
(gdb) c
Cont
inu
i
ng.
Breakpoi
n
t2
, ma
in(
)a
tave.c
:13
13 ave = sum/num;
3: sum = 10
2: va
l =4
1: num = 4
CS3411 Debugging 12
Using gdb (ave.c)
(gdb) n
14 p
r
in
t
f
(
"
Average i
s %f
n",ave)
;
3: sum = 10
2: va
l =4
1: num = 4
(gdb) pave
$1 = 2
(gdb) p(
doub
le)sum/(doub
le)num
$2 = 2
.5
(gdb) c
Cont
inu
i
ng.
Average i
s 2
.000000
Program ex
i
ted wi
th code 024.
(gdb) q
%
Everything fine until ave is
computed. Integer division the
problem.
Evaluate expression inside
gdb to validate our reasoning.
CS3411 Debugging 13
A GUI for gdb: ddd
• The ddd program is just a GUI front-end for gdb.
• Value added three main ways:
– Can mouse left on source line, then mouse left on Break at
(
)
to set a breakpoint. Or mouse right on a source line and set
a breakpoint in the menu that pops up.
– Can mouse left on variable, then mouse left on Pr
in
t
(
)or
Disp
lay(
)to examine data values. Or get value displayed at
bottom of ddd window by ``mouse hovering'' over a variable
name.
– Displayed values graphically displayed. Click on a pointer
value, graphically display thing pointed to. Visualize complex
linked data structures.
• Play with inorder tree traversal program.
CS3411 Debugging 14
Using ddd (inorder.c)
Introduce a pointer-related bug into the program by modifying
the inorder() function:
.....
vo
idinorder
(
r
)
s
t
ruc
t node *
r
;
{
inorder
(
r
->
le
f
t
)
;
p
r
in
t
f
(
"%c"
,
r
->data)
;
inorder
(
r
->r
ight
)
;
}
Formerly:
i
f(
r!= NILNODE){
inorder
(
r
->
le
f
t
)
;
p
r
in
t
f
(
"%c"
,
r
->data)
;
inorder
(
r
->r
ight
)
;
}
CS3411 Debugging 15
Quickie Post Mortem Debugging (inorder
.
c)
% a.out
Segmenta
t
ionfau
l
t(core dumped)
% gdb a.ou
tcore
GNU gdb 6
.1
..........
Core was generated by `
.
/a
.ou
tcore
'
.
Program te
rminated w
i
ths
igna
l 11, Segmenta
t
ion fau
l
t
.
..........
Read
ing symbo
l
sf
rom /
l
i
b/
t
l
s
/
l
ibc.
so
.6
.
.
.done.
Loaded symbolsfo
r/
l
i
b
/
t
l
s
/
l
ibc
.so.6
Read
ing symbo
l
sf
rom /
l
i
b/
ld
-
l
inux
.so
.2
.
.
.
done.
Loaded symbolsfo
r/
l
i
b
/
l
d-
l
inux
.so.2
#0 0x080484d5i
ninorder(
r=0x0) a
tbuggy_
inorder
.c
:38
38 inorder
(
r
->
le
f
t
)
;
(gdb)
Function in which segfaulted.
Arguments to function.
Line of source where segfaulted.
CS3411 Debugging 16
Quickie Post Mortem Debugging (inorder
.
c)
(gdb) where
#0 0x080484d5i
ninorder(
r=0x0) a
tbuggy_
inorder
.c
:38
#1 0x080484ddi
ninorder(
r=0x804a008) a
tbuggy_
inorder
.c
:38
#2 0x080484ddi
ninorder(
r=0x804a028) a
tbuggy_
inorder
.c
:38
#3 0x080484ddi
ninorder(
r=0x804a048) a
tbuggy_
inorder
.c
:38
#4 0x080484ddi
ninorder(
r=0x804a068) a
tbuggy_
inorder
.c
:38
#5 0x08048479i
n main(
)a
t buggy_
inorder
.c
:21
The above listing walks back the call chain as it was at the moment
of the segfault.
Clear that we dereferenced a null pointer in a call to i
norder
(
)at
a leaf node of the binary tree.
CS3411 Debugging 17
CS3411 Debugging 18
CS3411 Debugging 19
CS3411 Debugging 20
Debugging Tips
• Examine the most recent change
• Debug it now, not later
• Read before typing
• Make the bug reproducible
• Display output to localize your search
• Write a log file
• Use tools
• Keep records

More Related Content

Similar to 05-Debug.pdf

Lab6 (pfl) 20_mdele136(ausaf)
Lab6 (pfl) 20_mdele136(ausaf)Lab6 (pfl) 20_mdele136(ausaf)
Lab6 (pfl) 20_mdele136(ausaf)
Ausaf Ahmad
 
Debugging Modern C++ Application with Gdb
Debugging Modern C++ Application with GdbDebugging Modern C++ Application with Gdb
Debugging Modern C++ Application with Gdb
SenthilKumar Selvaraj
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
Ji Hun Kim
 
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeksBeginning direct3d gameprogramming05_thebasics_20160421_jintaeks
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
JinTaek Seo
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
Data Acquisition
Data AcquisitionData Acquisition
Data Acquisition
azhar557
 
MapReduce Algorithm Design
MapReduce Algorithm DesignMapReduce Algorithm Design
MapReduce Algorithm Design
Gabriela Agustini
 
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
Spark Summit
 
C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
Programming Homework Help
 
c programing
c programingc programing
c programing
bibek lamichhane
 
Bti1022 lab sheet 3
Bti1022 lab sheet 3Bti1022 lab sheet 3
Bti1022 lab sheet 3alish sha
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7alish sha
 
Computer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming IComputer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming I
💻 Anton Gerdelan
 
Gdb tutorial-handout
Gdb tutorial-handoutGdb tutorial-handout
Gdb tutorial-handoutSuraj Kumar
 
Lab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsLab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer Graphics
Rup Chowdhury
 
Slicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsSlicing of Object-Oriented Programs
Slicing of Object-Oriented Programs
Praveen Penumathsa
 
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in JavaRuntime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
Juan Fumero
 
Graphic Design Lab File.docx
Graphic Design Lab File.docxGraphic Design Lab File.docx
Graphic Design Lab File.docx
PayalJindal19
 

Similar to 05-Debug.pdf (20)

Lab6 (pfl) 20_mdele136(ausaf)
Lab6 (pfl) 20_mdele136(ausaf)Lab6 (pfl) 20_mdele136(ausaf)
Lab6 (pfl) 20_mdele136(ausaf)
 
Debugging Modern C++ Application with Gdb
Debugging Modern C++ Application with GdbDebugging Modern C++ Application with Gdb
Debugging Modern C++ Application with Gdb
 
LMmanual.pdf
LMmanual.pdfLMmanual.pdf
LMmanual.pdf
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
 
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeksBeginning direct3d gameprogramming05_thebasics_20160421_jintaeks
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
Data Acquisition
Data AcquisitionData Acquisition
Data Acquisition
 
MapReduce Algorithm Design
MapReduce Algorithm DesignMapReduce Algorithm Design
MapReduce Algorithm Design
 
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
 
C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
c programing
c programingc programing
c programing
 
Bti1022 lab sheet 3
Bti1022 lab sheet 3Bti1022 lab sheet 3
Bti1022 lab sheet 3
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
 
Computer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming IComputer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming I
 
Gdb tutorial-handout
Gdb tutorial-handoutGdb tutorial-handout
Gdb tutorial-handout
 
Lab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsLab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer Graphics
 
Slicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsSlicing of Object-Oriented Programs
Slicing of Object-Oriented Programs
 
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in JavaRuntime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
 
02 c++g3 d (1)
02 c++g3 d (1)02 c++g3 d (1)
02 c++g3 d (1)
 
Graphic Design Lab File.docx
Graphic Design Lab File.docxGraphic Design Lab File.docx
Graphic Design Lab File.docx
 

Recently uploaded

Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 

Recently uploaded (20)

Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 

05-Debug.pdf

  • 2. CS3411 Debugging 2 Debugging Basics • Will focus on the gcc/gdb combination. • Will also talk about the ddd gui for gdb (lots of value added to gdb). • First, debugging in the abstract: – Program state is a snapshot of all variables, PC, etc. – A statement in your program transforms one program state into another. – You should be able (at some level) to express what you expect the state of your program to be after every statement. – Often state predicates on program state; i.e., “if control is here, I expect the following to be true.” • Map into a toy example.
  • 3. CS3411 Debugging 3 Small Example: ave.c # inc lude <s td io .h> i n tsum=0, va l ,num=0; doub le ave; main( ) { whi le(scanf ( "%dn" ,&val )!= EOF){ sum += va l ; num++; } i f(num > 0){ ave = sum/num; p r in t f ( "Averageis %f n" , ave) ; } } sum should be 0 and num should be 0. sum should be the total of the num input values and there is no more input. sum should be the total of the num input values processed. ave should be the floating point mean of the num input data values.
  • 4. CS3411 Debugging 4 Small Example: ave.c % a.out 1 Average i s 1 .000000 % a.out 1 2 3 Average i s 2 .000000 % a.out 1 2 3 4 Average i s 2 .000000 Experienced programmer can probably “eyeball debug” the program from this output
  • 5. CS3411 Debugging 5 Using gdb • Make sure to compile source with the -g switch asserted. • In our case, gcc -gave.c • Breakpoint: line in source code at which debugger will pause execution. At breakpoint, can examine values of relevant components of program state. break command sets a breakpoint; c l ear removes the breakpoint. • Diagnostic pr in t f ( )crude, but effective way of getting a snapshot of program state at a given point. • Once paused at a breakpoint, use gdb pr in t, or displ ay to show variable or expression values. disp lay will automatically print values when execution halts at breakpoint. • From a breakpoint, may step or nextto single step the program. s tep stops after next source line is executed. nextsimilar, but executes functions without stopping.
  • 6. CS3411 Debugging 6 Using gdb • May find out where execution is, in terms of function call chain, with the where command; also shows function argument values. • Apply some of this in context of bogus averaging program. • To make things easier, put the problematic data set in a file named data. % a.ou t < da ta Averagei s 2 .000000
  • 7. CS3411 Debugging 7 Using gdb (ave.c) % gdb a.ou t GNU gdb 6 .1 Copyr ight 2004 Free Software Foundat ion,Inc . GDB isf ree so f t ware, covered by the GNU Genera l Pub l i c L icense, and you a re we l come to change i tand/or d is t r ibu t e cop ies ofi tundercer ta in cond i t ions. Type "show copy ing"tosee the cond i t ions. Thereis abso lu t e ly no war ran tyf or GDB. Type " show war ran ty ” fo r de ta i l s . This GDB was conf igured as " i586-suse- l inux" . . . Using host l i b th read_dbl ib rary" / l i b / t l s / l ib th read_db.so.1" . (gdb)
  • 8. CS3411 Debugging 8 Using gdb (ave.c) (gdb)l 1 # incl ude <s td i o.h> 2 3 in tsum=0, va l ,num=0; 4 doub le ave ; 5 6 ma i n( ) 7 { 8 whi l e(scan f ( "%dn" ,&va l )!= EOF) { 9 s um += va l ; 10 num++; (gdb)l 11 } 12 i f(num > 0){ 13 ave = sum/num; 14 pr in t f ( "Averageis%fn" ,ave) ; 15 } 16 } 17 (gdb) Interesting point: top of main loop. Another interesting point: just before ave is computed.
  • 9. CS3411 Debugging 9 Using gdb (ave.c) (gdb) b reak 8 Breakpo i nt 1 a t 0x80483dc:f i l e ave .c ,l i ne 8 . (gdb) b reak 13 Breakpo i nt 2 a t 0x8048414:f i l e ave .c,l i ne 13 . (gdb) d isp lay num (gdb) d isp lay va l (gdb) d isp lay sum (gdb)r <data Star t ing program:/ home/ jmayo/courses .d … Breakpo i nt 1 , ma in ( )a tave . c :8 8 whi l e(scanf ( "%dn" ,&va l )!= EOF) { 3: sum = 0 2: va l = 0 1: num = 0 (gdb) c Cont inu ing. Breakpo i nt 1 , ma in ( )a tave . c :8 8 whi l e(scanf ( "%dn" ,&va l )!= EOF) { 3: sum = 1 2: va l = 1 1: num = 1
  • 10. CS3411 Debugging 10 Using gdb (ave.c) (gdb) c Cont inu i ng. Breakpoi n t1 , ma in( )a tave.c :8 8 wh i le(scanf ( "%dn" ,&va l )! = EOF) { 3: sum = 3 2: va l =2 1: num = 2 (gdb) c Cont inu i ng. Breakpoi n t1 , ma in( )a tave.c :8 8 wh i le(scanf ( "%dn" ,&va l )! = EOF) { 3: sum = 6 2: va l =3 1: num = 3
  • 11. CS3411 Debugging 11 Using gdb (ave.c) (gdb) c Cont inu i ng. Breakpoi n t1 , ma in( )a tave.c :8 8 wh i le(scanf ( "%dn" ,&va l )! = EOF) { 3: sum = 10 2: va l =4 1: num = 4 (gdb) c Cont inu i ng. Breakpoi n t2 , ma in( )a tave.c :13 13 ave = sum/num; 3: sum = 10 2: va l =4 1: num = 4
  • 12. CS3411 Debugging 12 Using gdb (ave.c) (gdb) n 14 p r in t f ( " Average i s %f n",ave) ; 3: sum = 10 2: va l =4 1: num = 4 (gdb) pave $1 = 2 (gdb) p( doub le)sum/(doub le)num $2 = 2 .5 (gdb) c Cont inu i ng. Average i s 2 .000000 Program ex i ted wi th code 024. (gdb) q % Everything fine until ave is computed. Integer division the problem. Evaluate expression inside gdb to validate our reasoning.
  • 13. CS3411 Debugging 13 A GUI for gdb: ddd • The ddd program is just a GUI front-end for gdb. • Value added three main ways: – Can mouse left on source line, then mouse left on Break at ( ) to set a breakpoint. Or mouse right on a source line and set a breakpoint in the menu that pops up. – Can mouse left on variable, then mouse left on Pr in t ( )or Disp lay( )to examine data values. Or get value displayed at bottom of ddd window by ``mouse hovering'' over a variable name. – Displayed values graphically displayed. Click on a pointer value, graphically display thing pointed to. Visualize complex linked data structures. • Play with inorder tree traversal program.
  • 14. CS3411 Debugging 14 Using ddd (inorder.c) Introduce a pointer-related bug into the program by modifying the inorder() function: ..... vo idinorder ( r ) s t ruc t node * r ; { inorder ( r -> le f t ) ; p r in t f ( "%c" , r ->data) ; inorder ( r ->r ight ) ; } Formerly: i f( r!= NILNODE){ inorder ( r -> le f t ) ; p r in t f ( "%c" , r ->data) ; inorder ( r ->r ight ) ; }
  • 15. CS3411 Debugging 15 Quickie Post Mortem Debugging (inorder . c) % a.out Segmenta t ionfau l t(core dumped) % gdb a.ou tcore GNU gdb 6 .1 .......... Core was generated by ` . /a .ou tcore ' . Program te rminated w i ths igna l 11, Segmenta t ion fau l t . .......... Read ing symbo l sf rom / l i b/ t l s / l ibc. so .6 . . .done. Loaded symbolsfo r/ l i b / t l s / l ibc .so.6 Read ing symbo l sf rom / l i b/ ld - l inux .so .2 . . . done. Loaded symbolsfo r/ l i b / l d- l inux .so.2 #0 0x080484d5i ninorder( r=0x0) a tbuggy_ inorder .c :38 38 inorder ( r -> le f t ) ; (gdb) Function in which segfaulted. Arguments to function. Line of source where segfaulted.
  • 16. CS3411 Debugging 16 Quickie Post Mortem Debugging (inorder . c) (gdb) where #0 0x080484d5i ninorder( r=0x0) a tbuggy_ inorder .c :38 #1 0x080484ddi ninorder( r=0x804a008) a tbuggy_ inorder .c :38 #2 0x080484ddi ninorder( r=0x804a028) a tbuggy_ inorder .c :38 #3 0x080484ddi ninorder( r=0x804a048) a tbuggy_ inorder .c :38 #4 0x080484ddi ninorder( r=0x804a068) a tbuggy_ inorder .c :38 #5 0x08048479i n main( )a t buggy_ inorder .c :21 The above listing walks back the call chain as it was at the moment of the segfault. Clear that we dereferenced a null pointer in a call to i norder ( )at a leaf node of the binary tree.
  • 20. CS3411 Debugging 20 Debugging Tips • Examine the most recent change • Debug it now, not later • Read before typing • Make the bug reproducible • Display output to localize your search • Write a log file • Use tools • Keep records