Object-oriented Programming in C++

Bachelor of Science, École Polytechnique

Surviving with GDB

If you are a Mac user, you have to use lldb instead of gdb. You can find the basic commands on this page. For the Linux and Windows/WSL users, you can use the commands provided here.

The commands every padawan should know

Launching GDB (in the terminal)

gdb [prog] Loads the program [prog] into GDB.
gdb --args [prog] [arg1 arg2 ...] Loads the program [prog] into GDB with the parameters [arg1 arg2 ...]
gdb [prog] [core-file] Loads the core dump of the program [prog] into GDB.
q or quit Quits GDB.

Showing the code (in gdb)

layout src Splits the view and shows the code.

Starting Program Execution (in gdb)

r or 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 ...] Sets the list of arguments ([arg1 arg2 ...]) for the next program to start.

Examining the State of a Process (in gdb)

p [var] or print [var] Displays the value of the variable [var].
p/x [var] Displays the hexadecimal value of the variable [var].
display [var] Displays the value of the variable [var] at every stop of the program.
bt or backtrace Displays the call stack.
frame Displays the current call frame.
frame [x] Selects the call frame [x].
l or list Displays the code around the selected call frame.

Monitoring Process Execution (in gdb)

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

Controlling Process Execution (in gdb)

n or next Steps forward one line.
s or step Steps forward one line. If the instruction to execute is a function call, it does not descend into the function.
c or continue Continues execution until the next breakpoint.

Becoming a GDB Jedi Master

Assembler (in gdb)

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

Reverse Debugging (in gdb)

record Starts recording the process behavior.
record stop Stops the recording.
rs or reverse-step Like step, but backwards.
rn or reverse-next Like next, but backwards.
rc or reverse-continue Like continue, but backwards.
set can-use-hw-watchpoints 0 Allows using watchpoints in reverse debugging.

Others (in gdb)

break [pos] if [cond] Stops the process at [pos] if the condition [cond] is met.
up Moves up one call frame in the stack.
down Moves down one call frame in the stack.
attach [pid] Attaches GDB to the process with pid [pid].
detach Detaches the process from GDB.
set env [var]=[value] Sets the value [value] to the environment variable [var].