Sub topic 9
Frame pointers or DWARF: getting complete stacks
This chapter assumes you can build a Rust project in release mode and run perf or cargo flamegraph on Linux. You should know what a call stack is. It explains how a profiler turns a sampled instruction pointer into a full chain of callers, why that sometimes fails, and how to choose between the two main methods: frame pointers and DWARF.
Why a stack sample can be incomplete
A sampling profiler interrupts your program many times per second. At each interrupt it records the current instruction address. For a flamegraph that is not enough. It also needs the caller of the current function, the caller's caller, and so on up to main. Recovering that chain is called stack unwinding (or stack walking).
The CPU does not keep a list of callers. The return addresses sit in memory on the stack, mixed with local variables, saved registers and padding. To find them, the unwinder needs to know where each function's frame starts and ends. There are two common ways to know this:
- Follow a chain of saved frame pointers.
- Interpret DWARF unwind tables, which describe each frame's layout.
If unwinding fails, the stack is cut short. The flamegraph then shows towers that are too short, or samples attached to the wrong parents, or a wide [unknown] frame. These errors do not stop the tool. They produce a graph that looks plausible and is wrong.
Frame pointers
On x86-64, register rbp can serve as a frame pointer. A function that uses it begins by pushing the caller's rbp onto the stack, then setting rbp to the current stack position. Each frame therefore holds a pointer to the previous frame, next to the return address. This forms a linked list through the whole stack.
Unwinding is then cheap. The kernel reads the return address, follows the saved rbp to the next frame, and repeats. This takes a few memory reads per frame, and it can run inside the interrupt handler.
The catch is that Rust does not always keep frame pointers. In optimized builds, the compiler is free to use rbp as a general-purpose register, because that gives the optimizer one more register. When that happens, the chain is broken. The same applies to the precompiled standard library and to C libraries you link against, which may have been built without frame pointers.
To ask Rust to keep them, set a compiler flag:
RUSTFLAGS="-C force-frame-pointers=yes" cargo build --releaseThen record with frame-pointer unwinding, which is the default for perf record -g:
perf record -F 99 -g -- ./target/release/myappThe cost of this approach is small. You lose one register, and each function has a few extra instructions in its prologue and epilogue. In most programs the slowdown is a few percent or less, but it is not zero, and you are profiling a binary that differs slightly from your normal release build.
DWARF unwinding
DWARF is the debug information format used on Linux. Besides line numbers and types, it can contain call frame information (CFI). For every instruction address in a function, CFI says how to compute the caller's stack pointer and return address. This works even when no register is reserved as a frame pointer.
When Rust compiles with debug = true, it emits this data. Linux also ships a similar section called .eh_frame, which is present even without debug info because it is used for exception handling and panics.
You ask perf to use it with:
perf record -F 99 --call-graph dwarf -- ./target/release/myappBecause the kernel cannot easily interpret DWARF in an interrupt, perf does something different. At each sample, it copies a chunk of the raw stack (8 KiB by default) into the recording. Later, in user space, perf script or perf report unwinds each copied stack using the DWARF tables. You can change the size:
perf record -F 99 --call-graph dwarf,16384 -- ./target/release/myappThis has several consequences:
- Large files. Every sample stores kilobytes, so
perf.datacan reach gigabytes for a long run. Lower the frequency or shorten the run. - Slow post-processing. Unwinding happens when you run
perf script, and it can take minutes. - Truncated deep stacks. If a stack is deeper than the copied chunk, the outer frames are lost. Deeply recursive code or heavy async nesting may need a larger size, which makes the files larger still.
- Higher overhead. Copying the stack at every sample slows the target more than frame-pointer unwinding.
On the other side, you do not need to rebuild with special flags beyond debug info, and it usually works through libraries that lack frame pointers, as long as their unwind tables are installed.
Comparing the methods
| Property | Frame pointers | DWARF |
|---|---|---|
| `perf` option | `-g` (or `--call-graph fp`) | `--call-graph dwarf` |
| Build requirement | `-C force-frame-pointers=yes` | `debug = true` (or `.eh_frame` present) |
| Data stored per sample | A short list of addresses | A copy of the stack, 8 KiB by default |
| Run-time overhead | Low | Higher |
| `perf.data` size | Small | Large |
| Works through libraries built without frame pointers | No, chain breaks | Usually, if unwind tables exist |
| Deep stack limit | Very deep stacks are fine | Limited by the copied size |
A practical rule is to start with frame pointers, because they are fast and give small files. Switch to DWARF when the graph shows signs of broken stacks that you cannot fix by rebuilding.
There is a third option on recent hardware. Some Intel CPUs support LBR (last branch record) and perf record --call-graph lbr uses it. It has no software overhead, but the depth is limited to the hardware's buffer (typically 16 to 32 entries), and it is not available in many virtual machines.
Recognizing broken stacks
Look for these symptoms in the flamegraph:
- A very wide frame named
[unknown]or a raw hex address. - Many separate short towers at the left or right, each starting with a library function instead of
main. - A function that appears at the bottom of the graph although you know it is called from deeper code.
- Total widths for
mainthat are much smaller than the whole graph.
You can inspect raw samples to confirm. Print a few stacks with perf script | head -n 60. A healthy sample ends in something like main, std::rt::lang_start and __libc_start_main. A broken one ends after one or two frames.
Fixing the common causes
- The standard library is missing frame pointers. The prebuilt
stdis compiled without them on many targets, so your chain may break insidestdcalls such as iterator internals that were not inlined. Use DWARF, or rebuildstdwith-Zbuild-stdon nightly with the frame-pointer flag. - A C dependency has no frame pointers. Rebuild it with
-fno-omit-frame-pointer, which for-syscrates can often be set through theCFLAGSenvironment variable. Otherwise use DWARF. - Stripped binaries. If
strip = trueappears in your release profile, names disappear. Keep the profiling build unstripped. - The stack is too deep for DWARF. Raise the copy size, for example
--call-graph dwarf,32768, and accept the larger file. - Missing symbols for shared libraries. Install the debug packages for your distribution's libraries, or
perfcannot map addresses to names.
When using cargo flamegraph, you can pass the call-graph mode through its options. For example:
cargo flamegraph --bin myapp -c "record -F 99 --call-graph dwarf"If you leave -c out, cargo flamegraph uses its own defaults, which are frame pointers on most setups. Check the version you have installed with cargo flamegraph --help.
A worked check
Use the slow prime counter from the earlier chapters. Add debug info to the release profile:
[profile.release]
debug = trueRecord twice, once with each method, and compare the output:
cargo build --release
perf record -F 99 -g -o fp.data -- ./target/release/myapp
perf record -F 99 --call-graph dwarf -o dwarf.data -- ./target/release/myapp
ls -lh fp.data dwarf.data
perf script -i fp.data | head -n 30
perf script -i dwarf.data | head -n 30Here the build has no force-frame-pointers flag. The first recording may therefore show truncated stacks, since optimized code is free to reuse rbp. The second should reach main. The DWARF file will be much larger. Rebuild with RUSTFLAGS="-C force-frame-pointers=yes" and record with -g again. You should see complete stacks and a small file.
For a tiny program like this one, is_prime may be inlined into count_primes, so the stacks are short in both cases. That is normal and is covered in the chapter on inlining. What matters here is that main appears at the root.
Summary
- Flamegraphs need whole stacks, so the profiler must unwind them.
- Frame pointers give cheap, compact stacks but must be present in your code and in the libraries you call.
- DWARF works without frame pointers, at the price of larger files, slower processing and a depth limit set by the copied stack size.
- Always inspect a few raw samples to confirm the stacks reach
main.
Exercise
Your flamegraph has a wide [unknown] frame, and many towers start at library functions instead of main. The binary was built with cargo build --release and no extra flags. Name two changes you could try, and one downside of each.
Show answer
Option 1: rebuild with RUSTFLAGS="-C force-frame-pointers=yes" and record with perf record -g. The downside is a small slowdown, and libraries built without frame pointers may still break the chain. Option 2: record with perf record --call-graph dwarf and keep debug = true in the release profile. The downside is large perf.data files, slower post-processing and truncated stacks if the stack is deeper than the copied chunk.
Exercise
Why does perf record --call-graph dwarf copy part of the raw stack into the recording, instead of unwinding at sample time like frame-pointer mode does?
Show answer
Walking DWARF unwind tables is too complex and slow to do safely inside the kernel's interrupt handler. So perf saves a chunk of the stack (8 KiB by default) with each sample and unwinds it later in user space, when you run perf script or perf report.