GDB Tricks

February 15, 2009 by hejian

Define macros into ~/.gdbinit :)

Getting help:

(gdb) help
(gdb) help show
(gdb) help info

Check code:

(gdb) b main
set a breakpoint at main function
(gdb) r
start the program under gdb
(gdb) bt
displays a stack frame for each active subroutine.
(gdb) x/i $pc
print the instruction to be execute

A macro to print flag register:

define flags
if (($eflags >> 0xB) & 1 )
printf "O "
else
printf "o "
end
if (($eflags >> 0xA) & 1 )
printf "D "
else
printf "d "
end
if (($eflags >> 9) & 1 )
printf "I "
else
printf "i "
end
if (($eflags >> 8) & 1 )
printf "T "
else
printf "t "
end
if (($eflags >> 7) & 1 )
printf "S "
else
printf "s "
end
if (($eflags >> 6) & 1 )
printf "Z "
else
printf "z "
end
if (($eflags >> 4) & 1 )
printf "A "
else
printf "a "
end
if (($eflags >> 2) & 1 )
printf "P "
else
printf "p "
end
if ($eflags & 1)
printf "C "
else
printf "c "
end
printf "\n"
end
document flags
Print flags register
end

A macro to print registers:

define reg
printf " eax:%08X ebx:%08X ecx:%08X edx:%08X eflags:%08X\n", $eax, $ebx, $ecx, $edx, $eflags
printf " esi:%08X edi:%08X esp:%08X ebp:%08X eip:%08X\n", $esi, $edi, $esp, $ebp, $eip
printf " cs:%04X ds:%04X es:%04X", $cs, $ds, $es
printf " fs:%04X gs:%04X ss:%04X ", $fs, $gs, $ss
flags
end
document reg
Print CPU registers
end

Check stack information:

(gdb) f
prints a brief description of the currently selected stack frame.
(gdb) info f
prints a verbose description of the selected stack frame.

Check stack data:

(gdb) print $esp
$1 = (void *) 0xffd58b64
(gdb) x/4x 0xffd58b64
0xffd58b64: 0xffd58b80 0xffd58bd8 0xf7e0adec 0xf7f58cc0
(gdb) x/4x $esp

Leave a Reply

You must be logged in to post a comment.

Wordpress template made by HeJian