Program Introspection

Reddit Discussion

Debugging

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:

GDB/LLDB

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:

PDB

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.

Web browser Developer Tools

Another example of a debugger, this time with a graphical interface.

strace

Observe system calls a program makes: strace {program}.

Profiling

Types of profiling: CPU, memory, etc.

Simplest profiler: time.

Go

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

Perf

Basic performance stats: perf stat {command}

Run a program with the profiler: perf record {command}

Analyze profile: perf report


Edit this page.