Sub topic 9
Frame pointers or DWARF: getting complete stacks
This chapter assumes you can build a Rust release binary, run perf on Linux, and know what a call stack is. You should also have read the earlier chapters on sampling and on building with debug symbols.
Why stacks can break
A sampling profiler records the call stack of the running thread each time it fires. To do this it must unwind the stack, which means walking from the current function back to its caller, then that function's caller, until it reaches main. If the walk fails partway, the sample is cut short. The flamegraph then shows frames that float at the wrong depth, or a wide [unknown] block, and the time is attributed to the wrong place.
There are two common ways to unwind.
Frame pointers
Many compilers keep a register (rbp on x86-64) that points to the current function's stack frame. Each frame stores the previous frame pointer, so the frames form a linked list. Unwinding is just following that list. It is fast, and the kernel can do it inside perf.
Rust's optimizer is free to reuse that register for ordinary values. In release builds, frame pointers are usually omitted, and the standard library you link against is prebuilt that way too. You can ask the compiler to keep them:
RUSTFLAGS="-C force-frame-pointers=yes" cargo build --release
perf record -F 99 --call-graph fp -- ./target/release/myappThe cost is small. One register is lost to the frame list, and each function does a little extra work on entry. Most programs slow down by a few percent at most.
DWARF unwinding
DWARF is the debug format that describes, for each instruction address, how to find the caller's frame. It does not need a frame pointer. If your binary has debug info (debug = true in [profile.release]), perf can use it:
perf record -F 99 --call-graph dwarf -- ./target/release/myappWith this mode the kernel copies a chunk of the raw stack (8 KB by default) into every sample. The unwinding happens later, in user space, when you run perf script. This has three consequences:
- The recording is much larger, so
perf.datacan reach gigabytes. - Post-processing is slow.
- Stacks deeper than the copied chunk are truncated. You can raise the size with
--call-graph dwarf,16384, at the price of even bigger files.
cargo flamegraph uses frame pointers by default on Linux. You can switch it with --post-process options or by passing perf arguments, but check cargo flamegraph --help for the flags in your installed version.
Choosing between them
| Row | Frame pointers | DWARF |
|---|---|---|
| Needs rebuild with flag | Yes | No, only debug info |
| Sample overhead | Low | High |
| `perf.data` size | Small | Large |
| Deep stacks | Complete | Truncated at copy size |
| Code without frame pointers (prebuilt libraries) | Broken stacks | Works if debug info exists |
Start with frame pointers. Switch to DWARF when you cannot rebuild everything, or when a library you depend on is built without frame pointers.
Common errors
- Wide
[unknown]frames or stacks that start mid-program. The binary or a library lacks frame pointers. Rebuild withforce-frame-pointers=yes, or use DWARF. - Flat, shallow graphs where you expect depth. The default
perf recordwithout-gor--call-graphrecords no stacks at all. - DWARF stacks cut off near the root. The copy size is too small. Increase it.
- Standard library frames missing parent calls. The prebuilt standard library omits frame pointers. This is usually harmless, but if it matters, use DWARF or build std with
-Zbuild-stdon nightly.
Exercise
You recorded with perf record -g on a release build that has debug = true but no frame pointer flag. The flamegraph shows many short, disconnected towers. Give two ways to fix it.
Show answer
The stacks break because the binary omits frame pointers, so unwinding stops early. Either rebuild with RUSTFLAGS="-C force-frame-pointers=yes" and record again, or record with --call-graph dwarf, which uses the debug info you already have.