marp |
---|
true |
- To follow along:
- Connect to
stu
via SSH git clone https://github.com/lam2mo/uug-debug.git
- Connect to
- Software defect: an error in code that produces incorrect behavior
- Colloquially called “bugs”
- Mismatch between user expectations and machine behavior
- Proximate cause (symptom) vs. root cause (defect)
- Debugging: working from the former towards the latter
- Basically: the process of continually asking “why is this happening?”
- One of the most important practical skills in programming
- Debugger (e.g.,
gdb
): a program that examines another running program- Execute the program step-by-step (by line or instruction)
- Examine the contents of memory at any point
- Add breakpoints and watchpoints
- Debuggers are more useful with extra information from the compiler
- In gcc, compile with the
-g
option to enable this - It’s also useful to disable optimization (
-O0
)
- In gcc, compile with the
gdb ./program
- launch GDB on program- (include
--tui
for "graphical" interface)
- (include
help <cmd>
- get description of a commandrun <args>
- begin/restart executionstart <args>
- begin/restart execution and pause at mainquit
- exit GDB
break <func>
- set a breakpoint at the beginning of a functionbreak <file>:<line>
- set a breakpoint at a specific line of codewatch <loc>
- pause when a specific variable or memory location changescontinue
- resume execution (until a breakpoint, watchpoint, or segfault)next
- run one line of code then pause (skips over function calls)step
- run one line of code then pause (descends into functions)nexti
/stepi
- likenext
andstep
but run one machine instructionfinish
- run until current function returns
print <expr>
- print current value of a variable or expressionprint /x <expr>
- same as above but print in hexadecimalptype <expr>
- print the type of a variable or expressiondisplay <expr>
- print value after every pauseinfo reg
- print values of all machine registersinfo locals
- print values of all local variablesbacktrace
- print stack trace (list of active functions on the stack)- (
up
anddown
to cycle through function call sites)
- (
x/<N><u><f> <addr>
- examine memory at given address- N is the number of units to display
- u is the unit type:
b
(byte)h
(half word - 2 bytes)w
(word - 4 bytes)g
("giant" word - 8 bytes)
- f is the format (any
printf
format ori
for machine instructions)x
for hexadecimal is particularly useful here
- GDB Tutorial
- Cheat sheets: simpler and denser
- Valgrind Tutorial
- LLDB tutorial
- GDB to LLDB command map