1Ifyougetwarningsfromgdbaboutbeingunableto12ndasymbolyoucanignorethem2 Herewehavestoppedatthe12rstbreakpointinthemainroutineWhenexecutionstopswecanexaminethecontentsofthestackoranylocalorg ID: 955906
Download Pdf The PPT/PDF document "filesSpecifyingandexaminingfilesinternal..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
files--Specifyingandexaminingfilesinternals--Maintenancecommandsobscure--Obscurefeaturesrunning--Runningtheprogramstack--Examiningthestackstatus--Statusinquiriessupport--Supportfacilitiestracepoints--Tracingofprogramexecutionwithoutstoppingtheprogramuser-defined--User-definedcommandsType"help"followedbyaclassnameforalistofcommandsinthatclass.Type"help"followedbycommandnameforfulldocumentation.Commandnameabbreviationsareallowedifunambiguous.Atthispoint,youarereadytoexecutetheprogram,setbreakpoints,andprintvariables,etc.Todebugaprogram,youmustrstrunit.The\run"commandallowsyoutodojustthis.Beforeyouu
seit,youmightwanttoknowmoreabout\run"usingthe\help"facility:(gdb)helprunStartdebuggedprogram.Youmayspecifyargumentstogiveit.Argsmayinclude"*",or"[...]";theyareexpandedusing"sh".Inputandoutputredirectionwith"","or",-5;─",-5;─""arealsoallowed.%Withnoarguments,usesargumentslastspecified(with"run"or"setargs").Tocancelpreviousargumentsandrunwithnoarguments,use"setargs"withoutarguments.(gdb)Asthehelptextindicates,runningtheprogramisthesameasexecutingintheshellandyoucanusethesameC-shellredirectionoperationsaswell.Inthecaseofbug.C,thereisnoinput,sowejusttype\run"withnoarguments:(gdb)runStart
ingprogram:/nfs/home/nsa/kena/csci3308/tmp/bugFirstcallsumis6Secondcallsumis6Programexitednormally.(gdb)Here,weseethattheprogramisnotexecutingcorrectly(theresultofeachcalltosumshouldbe15).1Wearenowreadytostartdebugging.SettingBreakpoints.Tounderstandwhatyourprogramisdoing,youwillwanttostopitatdierentpointsinitsexecution.Breakpointsallowyoutodo 1Ifyougetwarningsfromgdbaboutbeingunabletondasymbol,youcanignorethem.2 Herewehavestoppedattherstbreakpoint,inthemainroutine.Whenex-ecutionstops,wecanexaminethecontentsofthestackoranylocalorglobalvariables.Forexample,the\backtrace"commandprintsthec
urrentfunctionsonthestack.Inthiscase,sincewejustenteredmain,thereisonlyone(youcanignorethestackframecausedbylibclibrary):(gdb)backtrace#0main()atbug.C:25#10x420156a4in__libc_start_main()from/lib/tls/libc.so.6(gdb)Ifwewishtocontinuefromabreakpoint,weusethe\continue"command.Notethatthisisdierentfrom\run"whichstartstheprogramfromthebeginningagain.(gdb)continueContinuing.Breakpoint3,sum(int*,int)(array_arg=0x8049a40,size=5)atbug.C:1515for(i=1;isize;i+=1){(gdb)Hereweseethatwehavestoppedatthesecondbreakpoint,insidethefunctionsumthatwascalledfrommain.Ifwenowask\backtrace",weseebothmainandsumonthecallsta
ck:(gdb)bt#0sum(int*,int)(array_arg=0x8049a40,size=5)atbug.C:15#10x0804879ainmain()atbug.C:25#20x420156a4in__libc_start_main()from/lib/tls/libc.so.6(gdb)Notethatthebacktracecommandalsoprintstheargumentstothefunctionsonthecallstack.Tondoutmoreaboutexaminingthestack,usethe\helpstack"command:Examiningthestack.Thestackismadeupofstackframes.Gdbassignsnumberstostackframescountingfromzerofortheinnermost(currentlyexecuting)frame.Atanytimegdbidentifiesoneframeasthe"selected"frame.Variablelookupsaredonewithrespecttotheselectedframe.Whentheprogrambeingdebuggedstops,gdbselectstheinnermostframe.Thecommandsbel
owcanbeusedtoselectotherframesbynumberoraddress.Listofcommands:backtrace--Printbacktraceofallstackframes4 (gdb)step16result+=array_arg[i];(gdb)Therearetwocommandstostepaheadastatement:\step"and\next".Thedierencebetweentheseisthatnextwillstepoverfunctioncalls,whilestepwillstep\into"functioncalls.Toseethedierence,supposewedeleteallbreakpoints,setabreakpointatline27inbug.C,andstarttheprogramfromthebeginning:(gdb)infobreakpointsNumTypeDispEnbAddressWhat1breakpointkeepy0x00004004inmainatbug.C:25breakpointalreadyhit1time3breakpointkeepy0x00003f8cinsum(int*,int)atbug.C:15breakpointalreadyhit1time(g
db)d13(gdb)breakbug.C:25Breakpoint4at0x8048786:filebug.C,line25.(gdb)rTheprogrambeingdebuggedhasbeenstartedalready.Startitfromthebeginning?(yorn)yStartingprogram:/nfs/home/nsa/kena/csci3308/tmp/bugBreakpoint4,main()atbug.C:2525sum1=sum(a1,5);(gdb)Ifwesay\next"wewillstepoverthecalltosumendingupatthenextline:(gdb)next26cout"Firstcallsumis"sum1"\n";(gdb)However,ifwesay\step"wewillstepintothecalltosum.Herewereruntheprogramtoillustratethepoint:(gdb)rTheprogrambeingdebuggedhasbeenstartedalready.Startitfromthebeginning?(yorn)yStartingprogram:/nfs/home/nsa/kena/csci3308/tmp/bugBreakpoint4,main()atbug.C:2525su
m1=sum(a1,5);(gdb)stepsum(array_arg=0x400010e4,size=5)atbug.C:156 16result+=array_arg[i];1:result=1073797554(gdb)Hereweseethatresultchangesasitisupdated,butbecauseitmighthaveanimproperinitialvalue,wecannotrelyonthisfunctionworking.Thisleadsustomodifyoursourcele(resultinginthelebug1.C),addinginitializationtotheresultinsum.(Goaheadandmakethischange,initializeresulttozero.Youcanexitgdbusingthequitcommand.)Notethattodisplayavariable,wemustwaituntilweareatabreakpointwherethevariableisvisible.Forexample,ifwetrytodisplayresultbeforewehaveenteredthefunctionsum,gdbgivesusanerrormessage.nagg++
-g-obug1bug.Cnaggdbbug1(gdb)displayresultNosymbol"result"incurrentcontext.(gdb)bsumBreakpoint1at0x3f8c:filebug.C,line12.(gdb)Sowemustwaittoentersumbeforeresultcanbedisplayed:(gdb)rStartingprogram:/nfs/home/nsa/kena/csci3308/tmp/bug1Breakpoint1,sum(array_arg=0x400010e4,size=5)atbug1.C:1212result=0;(gdb)displayresult1:result=1073797552(gdb)n16for(i=1;isize;i+=1){1:result=0(gdb)n17result+=array_arg[i];1:result=0(gdb)n16for(i=1;isize;i+=1){1:result=2(gdb)Nowweseethatresultgetsaninitialvalue,butaswestartaddingarrayelementstoit,therstvalueaddedis2insteadof1.Thisleadsustotheo-by-oneloopinde
xbug.Ifwechangetheboundsofthearrayfrom1tosizeto0to(size-1)wehavexedthesecondbug.Makethischangenow.FinishingtheExample.Recompiletheprogramintobug2andstartthedebuggeronthisversionoftheprogram.8 #includestre; m.h;intdummy1=100;inta1[]={1,2,3,4,5};intdummy2=100;intsum(intarray_arg[],intsize){intresult,i;//Hereisthefirstbug:resultisneverinitialized.//Hereisthesecondbug:irangesfrom1tosize,not0to(size-1)for(i=1;isize;i+=1){result+=array_arg[i];}//Hereisthethirdbug:nothingisreturnedfromsum.}intmain(){intsum1,sum2;sum1=sum(a1,5);cout"Firstcallsumis"sum1"\n";sum2=sum(a1,5);cout"Secondcallsumis"sum2"\