The ~/.gdbinit
file is used to add default commands, breakpoints and user defined commands for GDB sessions. The file can be ~/.config/gdb/gdbinit
, ~/.gdbinit
or ./.gdbinit
for a particular project.
For example,
set auto-load safe-path /
set disassembly-flavor intel
break main
For a more complete option, see Gdbinit/gdbinit on GitHub.
For all of the following, append any program arguments to the command. For example, start <ARGV1> <ARGV2> <ARGVN> < <STDIN_PATH>
.
run
runs a program with no breakpointsstart
starts a program with a breakpoint onmain
starti
starts a program with a breakpoint on_start
attach <PID>
to attach to a running programcore <PATH>
to analyze a coredumpcontinue
orc
to continue execution after a breakpoint
info registers
to display all registersp $rdi
to print out the value of a registerp/x $rdi
to print out the value of a register in hex
x/<n><u><f> <address>
where,n.
is the number of elements to display.<u>
is the unit size to display. Valid unit sizes areb
(1 byte),h
(2 bytes),w
(4 bytes), andg
(8 bytes).<f>
is the format to display the data in. Valid formats ared
(decimal),x
(hexadecimal),s
(string) andi
(instruction).<address>
can be specified using a register name, symbol name, or absolute address, and mathematical expressions can be used.
disassemble <function>
ordisas <function>
will disassemble an entire funtion. For example,disas main
.- When disassembling, you can switch to Intel syntax with
set disassembly-flavor intel
x/8i $rip
will print the next 8 instructions from the current instruction pointer.x/16i main
will print the first 16 instructions of main.x/16gx $rsp
will print the first 16 values on the stack.x/gx $rbp-0x32
will print the local variable stored there on the stack.
When stepping through machine instructions, it is useful to first set display/i $pc
to automatically display the next instruction each time the program stops. You could also use display/4gx $rsp
to show the next four instructions.
You can also use layout regs
to put GDB into its TUI mode and show you the contents of all of the registers, as well as nearby instructions.
step [count]
ors
steps to the next source line. This is only useful if the program was compiled with debug information. Count is an optional repeat. It will step into calls.stepi [count]
orsi
steps one machine instruction. Count is an optional repeat. It will step into calls.next [count]
orn
works likestep
but it will step over function calls.nexti [count]
orni
works likestepi
but it will step over function calls.finish
orfin
continues until a stack frame returns and prints the return value if any. Think of it as step out.until
oru
is likefinish
but it is steps out of a loop. I.E. it will not jump back.break *<address>
sets a breakpoint at an address.
The set
command can be used to modify the state of a program. For example,
set $rdi = 0
will zero out the RDI registerset *((uint64_t *) $rsp) = 0x1234
will set the first value on the stack to 0x1234set *((uint16_t *) 0x31337000) = 0x1337
will set 2 bytes at 0x31337000 to 0x1337
Given a networked application which reads from some socket on fd 42. It might be easier if it instead read from stdin. This can be done with the following gdb script:
start
catch syscall read
commands
silent
if ($rdi == 42)
set $rdi = 0
end
continue
end
continue