Appendix: Quick
Reference
- Starting GDB
gdb <program>: debug the program
<program>
gdb <program> <core>: debug the program
<program> with core file
<core>
gdb -p <pid>: debug the running program with PID
<pid>
ulimit -S -c unlimited: allow the system to produce
core files of unlimited size [shell command, not gdb]
ulimit -S -c 0: limit core file sizes to 0 bytes (ie,
disable core dumps) [shell command, not gdb]
- Running a Program
run: run the program without command-line
arguments
run <arg1> [<arg2>...]: run the program
with command-line arguments
- Setting Breakpoints
break <function>: set a breakpoint at the first
instruction of <function>
break *<function> + <offset>: set a
breakpoint at the instruction <offset> bytes from the
start of function <function>
break <line>: set a breakpoint at line
<line>
break <file>:<line>: set a breakpoint at
line <line> in source file
<file>
- Controlling Execution
next: execute one source code statement in the current
function
next <num>: run next
<num> times
step: execute one source code statement, descending
into other functions if appropriate
step <num>: run step
<num> times
stepi: execute one instruction
stepi <num>: run stepi
<num> times
continue: run until a breakpoint is encountered or the
program terminates
- Examining the Code
disassemble: display the assembly code for the current
function
disassemble <function>: display the assembly code
for the function <function>
disassemble/r: like disassemble, but
includes the raw bytes of each instruction
disassemble/s: like disassemble, but
groups instructions with corresponding lines of source code, which are
also displayed
disassemble/rs: combines /r and
/s options
- Viewing the Backtrace
where, bt, backtrace:
displays the program’s stack frames, starting with the function
containing the instruction pointer
- Changing the Current Function View
up: change the active stack frame to the current
frame’s caller
down: change the active stack frame to the current
frame’s callee
- Inspecting Frame Information
info frame: display stack frame information for the
active stack frame
info frame <num>: display stack frame information
for the frame <num>, which is numbered the same as in
the backtrace
- Local Variables
info locals: display the current function’s local
variables, with values
info args: display the current function’s arguments,
with values
print <expression>: print the value of
<expression>
set variable <var> = <val>: set the
variable <var> to <val>
- Registers
info registers: display all registers and their
values
info registers <reg1> [<reg2> ...]: display
the specified registers and their values
print $<reg>: print the value of register
<reg>
set $<reg> = <val>: set the value of
register <reg> to <val>
- Examining Memory
x <addr-expression>: examine the memory specified
by <addr-expression>
x/<fmt> <addr-expression>: examine the
memory specified by <addr-expression> with the format
string <fmt>
- Format Strings
<N>: repeat count of <N>
items, where each item is determined by the format and size letters
- format letter
t: binary
o: octal
d: decimal
u: unsigned decimal
x: hexadecimal
z: 0-padded hexadecimal
f: floating-point
a: address
i: instruction
c: character
s: string
- size letter
b: bytes
h: half-word (2 bytes)
w: word (4 bytes)
g: giant (8 bytes)