You are on page 1of 24

AnIntroductiontoGDB

Introductionto GDB

1
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Introduction
GNUGNUisNotUNIX
RecursiveAcronym FreeSoftware

GCCGNUCompilerCollection
C,C++ ADA Fortran Java,etc.,

GDBGNUSymbolicDebugger
Supportswidevarietyoflanguages

2
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

GNUSymbolicDebugger
Adebuggerforseverallanguages,includingCandC++ Itallowsyoutoinspectwhattheprogramisdoingatacertain pointduringexecution. Errorslikesegmentationfaultsmaybeeasiertofindwiththe helpofgdb.

3
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Stepsrequiredduringcompilation
Normally,youwouldcompileaprogramlike:
gcc[flags]<sourcefiles>o<outputfile>

Forexample:
gccWallWerroransipedanticerrorsprog1.coprog1

Nowyouaddagoptiontoenablebuiltindebuggingsupport (whichgdbneeds):
gcc[otherflags]g<sourcefiles>o<outputfile>

Forexample:
gccWallWerroransipedanticerrorsgprog1.coprog1.x

4
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

GettinginandoutofGDB
GettingIn
gdb gdbprogram gdbprogrampid gdbprogramcore startGDB,withnodebuggingfiles begindebuggingprogram attachprogramtorunningpid debugcoredumpcoreproducedby program

GettingOut
quitorq Ctrld

Help
gdbhelp describecommandlineoptions
5
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Executingtheprogram
setargs<args...>
Setargumentstopasstoprogramtobedebugged.

run
Runtheprogramtobedebugged.

kill
Killtherunningprogram.

6
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

BreakpointsandWatchpoints[1/2]
break<where>
Setanewbreakpoint.

watch<where>
Setanewwatchpoint.

delete<breakpoint#>
Removeabreakpoint.

delete/enable/disable <watchpoint#>
Likebreakpoints.

clear
Deleteallbreakpoints.

enable<breakpoint#>
Enableadisabled breakpoint.

disable<breakpoint#>
Disableabreakpoint.
7
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

BreakpointsandWatchpoints[2/2]
function_name
Break/watchthenamed function.

break/watch<where>if <condition>
Break/watchatthegiven locationiftheconditionis met.Conditionsmaybe almostanyCexpressionthat evaluatetotrueorfalse.

line_number
Break/watchthelinenumber inthecurrentsourcefile.

condition<breakpoint#> <condition>
Set/changetheconditionof anexistingbreakor watchpoint.

file:line_number
Break/watchtheline

8
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Steppingthroughtheprogram[1/3]
step
Gotonextinstruction(sourceline),divingintofunction.

next
Gotonextinstruction(sourceline)butdontdiveintofunctions.

finish
Continueuntilthecurrentfunctionreturns.

continue
Continuenormalexecution.
9
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Steppingthroughtheprogram[2/3]
continue[count] c[count]
continuerunning;ifcountspecified,ignorethisbreakpointnextcounttimes

step[count] s[count]
executeuntilanotherlinereached;repeatcounttimesifspecified

stepi[count] si[count]
stepbymachineinstructionsratherthansourcelines

next[count] n[count]
executenextline,includinganyfunctioncalls 10
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Steppingthroughtheprogram[3/3]
nexti[count] ni[count]
nextmachineinstructionratherthan sourceline

signalnum
resumeexecutionwithsignals(noneif 0)

jumpline jump*address
resumeexecutionatspecifiedline numberoraddress

until[location]
rununtilnextinstruction(orlocation)

finish
rununtilselectedstackframereturns

setvar=expr
evaluateexprwithoutdisplayingit;use foralteringprogramvariables

return[expr]
popselectedstackframewithout executing[settingreturnvalue]

11
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

VariablesandMemory[1/3]
print/format<what>
Printcontentofvariable/ memorylocation/register.

x/nfu<address>
Printmemory. n:Howmanyunitstoprint (default1). f:Formatcharacter(likeprint) u:Unit. Unitisoneof:
b:Byte, h:Halfword(twobytes) w:Word(fourbytes) g:Giantword(eightbytes)).

display/format<what>
Likeprint,butprintthe informationaftereachstepping instruction.

undisplay<display#>
Removethedisplaywiththe givennumber.

enabledisplay<display#> disabledisplay<display#>
Enordisablethedisplaywith thegivennumber.

12

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

VariablesandMemory[2/3]
Formatforprint/display/x
a c d f o s t u x Pointer Readasinteger,printascharacter Integer,signeddecimal Floatingpointnumber Integer,printasoctal TrytotreatasCstring Integer,printasbinary(t=two) Integer,unsigneddecimal Integer,printashexadecimal

13
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

VariablesandMemory[3/3]
What
expression
AlmostanyCexpression,includingfunctioncalls(mustbeprefixed withacasttotellGDBthereturnvaluetype).

file_name::variable_name
Contentofthevariabledefinedinthenamedfile(staticvariables).

function::variable_name
Contentofthevariabledefinedinthenamedfunction(ifonthestack).

{type}address
Contentataddress,interpretedasbeingoftheCtypetype.

$register
Contentofnamedregister.Interestingregistersare$esp(stack pointer),$ebp(framepointer)and$eip(instructionpointer).
14
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

ProgramStack
backtrace[n]orbt[n]
printtraceofallframesin stack;orofnframes innermostifn>0,outermostif n<0

infoframe[addr]
describeselectedframe,or frameataddr

infoargs
argumentsofselectedframe

frame[n]
selectframenumbernor frameataddressn;ifnon, displaycurrentframe

infolocals
localvariablesofselected frame

upn
selectframenframesup

inforeg[rn]... infoallreg[rn]
registervalues[forregsrn]in selectedframe;allreg includesfloatingpoint

downn
selectframenframesdown

15

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Sources
directory<directory>
Adddirectorytothelistofdirectoriesthatissearchedforsources.

list list<filename>:<function> list<filename>:<line_number> list<first>,<last>


Showsthecurrentorgivensourcecontext.Thefilenamemaybe omitted.Iflastisomittedthecontextstartingatstartisprinted insteadofcenteredaroundit.

setlistsize<count>
Sethowmanylinestoshowinlist.
16
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Informations
disassemble disassemble<where>
Disassemblethecurrentfunctionor givenlocation.

infosharedlibrary
Listloadedsharedlibraries.

infosignals
Listallsignalsandhowtheyare currentlyhandled.

infoargs
Printtheargumentstothefunctionof thecurrentstackframe.

infothreads
Listallthreads.

infobreakpoints
Printinformationsaboutthebreakand watchpoints.

showdirectories
PrintalldirectoriesinwhichGDB searchesforsourcefiles.

infodisplay
Printinformationsaboutthedisplays.

showlistsize
Printhowmanyareshowninthelist command.

infolocals
Printthelocalvariablesinthecurrently selectedstackframe.

whatisvariable_name
Printtypeofnamedvariable. 17

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

LoggingOutput
setloggingon
Enablelogging

setloggingoff
Disablelogging

setloggingfilefile
Changethenameofthecurrentlogfile.Thedefaultlogfileisgdb.txt

setloggingoverwrite[on|off]
Bydefault,gdbwillappendtothelogfile.Setoverwriteifyouwantsetloggingontooverwrite thelogfileinstead

setloggingredirect[on|off]
Bydefault,gdboutputwillgotoboththeterminalandthelogfile.Setredirectifyouwant outputtogoonlytothelogfile

showlogging
Showthecurrentvaluesoftheloggingsettings 18

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

DebuggingRemotePrograms
gdbserver
Utilityrunsonthetarget Acceptscommandsfromhostmachine

gdb
Runsonhostmachine

19
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Runninggdbserver
gdbservercommprogram[args...]
commiseitheradevicename(touseaserialline)oraTCP hostnameandportnumber. Eg:gdbserver/dev/com1emacsfoo.txt gdbserverhost:2345emacsfoo.txt

gdbserverattachcommpid
pidistheprocessIDofacurrentlyrunningprocess.

20
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Connectingtoremotetarget
targetremote
targetremoteserialdevice Eg.targetremote/dev/tty1 targetremotehost:port targetremotetcp:host:port Eg.targetremote107.108.204.234:1234 targetremoteudp:host:port

21
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

FurtherStudy
Debuggingwithgdb
ThegnuSourceLevelDebugger By RichardStallman,RolandPesch,StanShebs,etal.

22
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Q?
23
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

ThankYou
24
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

You might also like