home ‣ Gdb basics login
Customization: File .gdbinit contains instructions that gdb will execute at startup.
Helpful for adding commonly used commands. Gdb first reads .gdbinit in $HOME directory,
then in current directory (if different than $HOME).
Gdb looks for source files in $cdir (directory embedded in executable recorded during
compilation) and $cwd (current working directory). You can add to this list via dir
e.g. dir ..\..\WebCore\platform. See current list via show dir. If you need this,
it's a perfect candidate for putting inside .gdbinit file.
Basics:
run [<args>]- run the program with (optional) argumentsc(continue) - continue executionbt- print a stack traceframe <n>- switch to frameinfo locals- show local variablesp <foo>- print the value of a variablex /fmt addr- show data under a given addr using a given formatinfo breaks- display current breakpointsbreak <line-no>- set breakpoint at a line numberin current file e.g. break 24break *<addr>- set a breakpoint at a given addr e.g.break *foo + 24(set a breakpoint 24 bytes after the beginning of function foodelete <n>,disable <n>,enable <n>- delete/disable/enable breakpoint
Debugging at assembly level:
display /i $eipso that gdb prints the next assembly instructionnextiandstepifor stepping by one instructionset disassembly-flavor intelchanges assembly syntax from awful AT&T to less awful intelinfo regs- show content of registers
Useful macros to define in .gdbinit:
dpc [<count>] disassembles next
define dpc
if $argc ==1
disass $pc $pc + $arg0
end
if $argc == 0
disass $pc $pc+24
end
end
pu <addr> print Unicode string under address
def pu
set $uni = $arg0
set $i = 0
while (*$uni && $i++<100)
if (*$uni < 0x80)
print *(char*)$uni++
else
print /x *(short*)$uni++
end
end
end