See also the GDB manual.
Handy GDB commands
Starting/stopping
run ARGS— start the program with the argumentsARGSrun ARGS <input.txt— start the program with the argumentsARGS, reading input from the fileinput.txtkill— terminate the current programstepi— step forward by one instructionnexti— step forward by one instruction, skipping any called functioncontinue— run until breakpoint
Breakpoint related
b function— set a breakpoint at entry to a particular functionb *0x123456— set a breakpoint at the instruction at address0x123456watch *(int*)0x123456— set a “watchpoint” that will trigger whenever memory at0x123456is updatedwatch *(int*)0x123456 == 42— set a “watchpoint” that will trigger whenever the int at address0x123456equals42(assuming it currently is not 42)rwatch *(int*)0x123456— set a “watchpoint” that will trigger whnever the program reads memory at0x123456info breakpoints,info watchpoints— view all active breakpoints/watchpointsdelete ID— clear breakpoint/watchpoint with the specified ID
Register examination
info registers— view all general purpose registersinfo all-registers— view all registers, even floating point registers, segment registers, etc.
Memory examination
x/100bx 0x12345678— print out 100 bytes of memory starting at address0x12345678, as a sequence of 1-byte hexadecimal numbersx/2gx 0x12345678— print out 16 bytes of memory starting at address0x12345678, as a two eight-byte hexdecimal numbersx/s 0x12345678— print out memory starting at address0x12345678as a\0-terminated stringx/wd 0x12345678— print out memory at address0x12345678as a single 4-byte decimal integer-
x/hd 0x12345678— print out memory at address0x12345678as a single 2-byte decimal integer -
x/100bx $rsi— print out 100 bytes of memory starting at the address in%rsi(you can similarly use$rsiin place of an address in the commands above) print *((long*) 0x12345678)— output the value of address0x12345678as a long (awkward way)- can also use an arbitrary C expression
display *((long*) 0x12345678)— output the value of address0x12345678as a long every time the program stopsundisplay ID— unset a previous display commandinfo displaylists active IDs
Disassembly related
disassemble function— disassemble a function by namedisassemble 0x10000,0x10050— disassemble whatever’s in memory between addresses0x10000and 0x10050`x/6i 0x10000— disassemble 6 instructions starting at address0x10000set disassembly-flavor att,set disassembly-flavor intel— switch between AT&T and Intel syntax assembly
Segfault related
p $_siginfo._sifields._sigfault.si_addr— (on Linux) Usually print out invalid memory access location after a segfault. Note that this may print out something bogus if a segfault did not occur. It also seems to sometimes print out0x0instead of the actual faulting address. A more tedious but more universal way to find out the faulting address is to examine the instruction that triggered the segfault withx/i $rip, and the register values it is using withinfo registers.