Using gdb
Whilethefocusofthisbookisnotonlearningfundamentalprogrammingtopicslikedebugging,
wewillbrieflyreviewusinggdbandsomesimpledebuggingparadigmsyoumayormaynot
haveheardbefore.Thisisinthehopesthatifyouarenewtoprogrammingorithasbeena
whileyouwillhaveabriefstartingpointfromwhichtobegin.Notethatwhilewearefocusingon
theGNUdebugger,thecommoncommandswewillcoveraresimilartoandfoundinalmostall
otherdebuggersincludingIDEsliketheMicrosoftVisualStudiobuiltindebugger.
TheGNUdebuggerisavailableonallplatforms,butismostreadilyavailableonlinux
distributionsfromthecommandline.Youwillusegdboften,orasimilartooltodebugyour
programswhenyoucomeupontroublesomeissuesyoucan’teasilyresolve.Youcanusegdb
tostepslowlythroughyourprograminordertolookatitalineorafewatatimetospecifywhere
andwhatyourproblemiswhileitisrunning.
Common Commands
run/r
Thiscommandrunsyourprograminsidethedebugger,oftenthiswillprintinformationrelevantto
wherethecrashedoccuredifyouhavenotsetabreakpointsincetheprogramwillsimplyrun
untilitfinishesorcrashes.Ifitcrashesyouwillfindtenextcommandquiteuseful.
list/l
Thiswilllistthelinesofcodenearwheretheprogramcrashed.Thiswilloftengiveyouagood
ideaofwheretostartlookingforerrorsbutitisimportanttonotethatthiswillcertainlynotalways
showyouthelineswheretherootoftheproblemlies,andmoreoftenthannotitwillnotshow
youthelinewheretheproblemactuallylies.
where
Thisisequivalenttoprintingabacktracefromwithintheprogram,allowingyoutoseethecall
stackthatledtotheprogramcrash.Thisishelpfulbyallowingyoutoseewhichfunctionswere
calledinwhatordertoleadtothecrash.
up
Thiscommandsimplymovesyouonestepbackupthecallstack
p/printVARIABLENAME
Thiscommandhastwomainuses.Youcanuseittoprintoutthewhateverthecurrentvalueof
avariableisatthetimeofexecution,oryoucanuseittochangethevalueofavariablethatis
currentlyinscopewhiletheprogramisrunning.Thiscanbeusefulinsomecasestodetermine
ifavariableisaccidentallyuninitialized,settotheincorrectvalue,etc.
b/breakpointLINE#orFUNCTION_NAME
© 2014 Gustavo Rodriguez-Rivera and Justin Ennen,Introduction to Systems Programming: a Hands-on Approach
(V2014-10-27) (systemsprogrammingbook.com)