ptrace

ptrace

The Linux syscall that lets one process inspect and control another. Foundation for every native-code debugger on Linux: GDB, LLDB, delve, rr.

man 2 ptrace. Standard since the 1980s. Hard to use directly; almost always wrapped by a debugger library.

What it can do

Process A (the debugger) calls ptrace on process B (the debuggee) to:

Why it's privileged

ptrace is dangerous: arbitrary memory read/write into another process. Linux restricts it via Yama (/proc/sys/kernel/yama/ptrace_scope) and capabilities. By default, you can ptrace your own processes; ptracing others requires CAP_SYS_PTRACE or root.

This is why some Docker setups need --cap-add=SYS_PTRACE or --security-opt seccomp=unconfined to debug inside containers.

Equivalents on other OSes

What ptrace doesn't do

Key insight

Most of "what a debugger does" is built from these three primitives:

Everything else (breakpoints, watchpoints, stepping, variable inspection) is the debugger library composing these primitives intelligently using DWARF as a map.

See also