layout | title | presenter | video | ||||
---|---|---|---|---|---|---|---|
lecture |
Program Introspection |
Anish |
|
When printf-debugging isn't good enough: use a debugger.
Debuggers let you interact with the execution of a program, letting you do things like:
- halt execution of the program when it reaches a certain line
- single-step through the program
- inspect values of variables
- many more advanced features
GDB and LLDB. Supports many C-like languages.
Let's look at example.c. Compile with debug flags:
gcc -g -o example example.c
.
Open GDB:
gdb example
Some commands:
run
b {name of function}
- set a breakpointb {file}:{line}
- set a breakpointc
- continuestep
/next
/finish
- step in / step over / step outp {variable}
- print value of variablewatch {expression}
- set a watchpoint that triggers when the value of the expression changesrwatch {expression}
- set a watchpoint that triggers when the value is readlayout
PDB is the Python debugger.
Insert import pdb; pdb.set_trace()
where you want to drop into PDB, basically
a hybrid of a debugger (like GDB) and a Python shell.
Another example of a debugger, this time with a graphical interface.
Observe system calls a program makes: strace {program}
.
Types of profiling: CPU, memory, etc.
Simplest profiler: time
.
Run test code with CPU profiler: go test -cpuprofile=cpu.out
Analyze profile: go tool pprof -web cpu.out
Run test code with CPU profiler: go test -memprofile=cpu.out
Analyze profile: go tool pprof -web mem.out
Basic performance stats: perf stat {command}
Run a program with the profiler: perf record {command}
Analyze profile: perf report