Implement a complete Python stack profiler that demonstrates how to:
- Walk CPython interpreter frame structures from eBPF
- Extract Python function names, filenames, and line numbers
- Combine native C stacks with Python interpreter stacks
- Profile Python applications with minimal overhead
Key features:
- Python internal struct definitions (PyFrameObject, PyCodeObject, PyThreadState)
- String reading for both PyUnicodeObject and PyBytesObject
- Frame walking with configurable stack depth
- Both human-readable and flamegraph-compatible output formats
- Command-line options for PID filtering and sampling frequency
Files added:
- python-stack.bpf.c: eBPF program for capturing Python stacks
- python-stack.c: Userspace program for printing results
- python-stack.h: Python internal structure definitions
- test_program.py: Python test workload
- run_test.sh: Automated test script
- README.md: Comprehensive tutorial documentation
- Makefile: Build configuration
- .gitignore: Ignore build artifacts
This tutorial serves as an educational foundation for understanding:
1. How to read userspace memory from eBPF
2. CPython internals and frame management
3. Sampling-based profiling techniques
4. Combining kernel and userspace observability
Note: Current implementation demonstrates concepts but requires
additional work for production use (thread state discovery,
multi-version support, symbol resolution).
🤖 Generated with Claude Code (https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>