Operating systems

Portail informatique

GDB cheat-sheet

The 15 commands to know for any padawan

To start / exit GDB

gdb [prog] Load the program [prog] into GDB.
gdb --args [prog] [arg1 arg2 ...] Load the program [prog] into GDB with the parameters [arg1 arg2 ...]
gdb [prog] [core-file] Load the core-dump of the program [prog] into GDB.
q ou quit Exit GDB.

Running a program

r ou run Starts the execution of the program loaded in gdb.
r [arg1 arg2 ...] Starts the execution of the program loaded in gdb with the parameters [arg1 arg2 ...] .
set [args arg1 arg2 ...] Select the list of arguments ( [arg1 arg2 ...] ) for the next program to start.

Examining a running process

Select the stack frameSelect the stack frame [x] . [x] .
p [var] ou print [var] Displays the value of the [var] variable.
p/x [var] Displays the hexadecimal value of the [var] variable.
display [var] Displays the value of the [var] variable each time the program stops.
bt ou backtrace Prints the call stack.
frame Prints the current stack frame .
frame [x] Select the stack frame [x] .
l ou list Print the portion of code around the selected stack frame .

Monitoring the execution of a process

b [pos] ou break [pos] Sets a breakpoint at the location [pos] . [pos] can be a function name, a line number (in the current file), a file name + line number ( break my_file: 57 ), etc.
clear [pos] Remove the breakpoint positioned at the [pos] location.
d [num] ou delete [num] Removes the breakpoint number [num] .
w [var] ou watch [var] Monitors the state of the [var] variable.

Control the execution of a process

n ou next Single step.
s or step Single step. If the instruction to be executed is a function call, do not descend in the function.
c ou continue Continue execution until the next breakpoint .

Threads

info threads Displays the list of threads running in the process.
thread [x] Positions on the [x] thread.
break [pos] thread [x] Adds a breakpoint to [pos] only for the thread [x] .

To become a GDB Jedi Master

Assembly

disassemble [function] Display the assembly code of the function [function] .
info registers Displays the value of registers.
p $[register] Displays the value of a register. Example: print $ eax .

Reverse debugging

record Starts recording the process execution.
record stop Stop recording.
rs ou reverse-step Same as step , but backwards.
rn ou reverse-next Same as next , but backwards.
rc ou reverse-continue Same as continue , but backwards.
set can-use-hw-watchpoints 0 Allows the use of watchpoints in reverse debugging.

Others

break [pos] if [cond] Stops the process at [pos] if the condition [cond] is true.
up Go to the next stack frame in the stack.
down Go to the previous stack frame in the stack.
attach [pid] Attach GDB to the process [pid] .
detach Detach a process from GDB.
set env [var]=[value] Assigns the value [value] to the environment variable [var] .

Other resources: