Why do profilers show broken call stacks for an -O2 GCC build, and what does -fno-omit-frame-pointer change?
answer
- a chain of saved frame pointers
- -O1 frees the register
- fp unwinding is cheap
- leaf functions and libraries
- one register, a few instructions
basics
~20 sFrom -O1 GCC enables -fomit-frame-pointer, so functions no longer keep the frame-pointer chain that cheap stack walking follows. -fno-omit-frame-pointer restores the chain at the cost of one register and a short prologue, giving profilers usable stacks.
solid answer
~50 sSampling profilers often reconstruct a call stack by following the **frame-pointer chain**: each function saves the caller's frame pointer and points the frame-pointer register at its own frame, so walking the chain is a few memory loads per sample. GCC enables `-fomit-frame-pointer` **at `-O1` and higher**, which drops that setup and frees the register, so on x86-64 Linux an `-O2` build breaks the chain and stacks come out truncated or wrong. `-fno-omit-frame-pointer` restores it. The cost is one register taken from the allocator and a push, move and pop per function, usually small. The manual adds that it does not guarantee a frame pointer in every function, since several targets omit it in leaf functions, and any library built without it still breaks the chain. The alternatives, DWARF-based unwinding or hardware branch records, avoid recompiling but cost more per sample or need specific CPUs.
code
plaintext · 14 lines# -O2 -S
_Z4worki:
pushq %r14
pushq %rbx
pushq %rax
...
# -O2 -fno-omit-frame-pointer -S
_Z4worki:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
...go deeper
Know that optimized builds drop frame pointers and that -fno-omit-frame-pointer brings them back for profiling.
Explain the frame-pointer chain, from which level GCC omits it, and what keeping it costs.
Diagnose truncated profiles from leaf functions or dependencies without frame pointers, and weigh frame pointers against DWARF unwinding.
Decide whether production binaries keep frame pointers so profiling is always possible, and apply the rule to dependencies too.
## How a frame-pointer stack walk works On x86-64 the conventional prologue is: ```plaintext push %rbp # save the caller's frame pointer mov %rsp, %rbp # this frame's base becomes the new frame pointer ... pop %rbp # restore on return ``` Each frame therefore stores a pointer to the previous frame next to the return address. A profiler that interrupts the program can read the frame-pointer register, then follow the saved pointers up the stack, collecting return addresses. That is **frame-pointer unwinding**: a handful of memory reads per sample, cheap enough to run thousands of times per second, and it works in the kernel without any debug data. ## What -O2 does to it GCC's manual lists `-fomit-frame-pointer` as **enabled by default at `-O1` and higher**. It omits the frame pointer in functions that do not need one, which avoids the save, set-up and restore instructions and, on many targets, gives the register allocator one more register. The register then holds ordinary data, so a profiler following it reads garbage: stacks stop early, skip callers or attribute samples to the wrong parent. | Build | Frame pointer kept? | Frame-pointer stacks | |---|---|---| | `-O0` | yes | correct, but the code is not representative | | `-O2` | no, by default | broken | | `-O2 -fno-omit-frame-pointer` | yes, in most functions | usable | The example shows the difference for one function compiled for x86-64: with `-fno-omit-frame-pointer` the prologue gains `push %rbp` and `mov %rsp, %rbp`. ## What -fno-omit-frame-pointer does not guarantee The manual is careful about its limits: - It **does not guarantee the frame pointer is used in all functions**; several targets always omit it in **leaf functions**, which call nothing. On x86, `-mno-omit-leaf-frame-pointer` asks for it there too. - On some targets the flag has no effect because the calling convention always uses a frame pointer, so it cannot be omitted. Those targets give good frame-pointer stacks at any level. - The chain is only as good as its weakest link. A system library or third-party dependency built without frame pointers breaks the walk at the point where execution enters it. ## The cost Keeping frame pointers costs: 1. **One register** taken from the allocator, which matters most on register-starved code; 2. a **push, a move and a pop** per call; 3. slightly larger code. For most applications the slowdown is small and hard to measure, which is why some teams build their production binaries with frame pointers so that profiling in production is always possible. For tight numeric kernels it can be measurable; measure before deciding. ## The alternatives - **DWARF-based unwinding**: the profiler copies a chunk of the stack per sample and unwinds it later with the `.eh_frame` or debug information. No rebuild is needed, but each sample costs far more and deep stacks may be truncated. - **Hardware branch records**: some CPUs record recent taken branches, from which a short call chain can be rebuilt. Cheap, but limited in depth and only on CPUs that have the feature. The choice of profiler and its options belongs to the performance material; the compiler side is simply whether the binaries keep the chain. ## Recognising a broken stack in a profile Frame-pointer damage has a recognisable look: - many samples attributed to a function's **caller's caller**, or to an unrelated function, because a data value in the register was read as a frame address; - stacks that stop after one or two frames, so the top-down view is mostly "unknown"; - hot functions that appear without any path back to `main` or the thread entry point; - clean stacks in your code that break exactly where they enter a dependency. The quickest confirmation is to rebuild the component with `-fno-omit-frame-pointer` and profile the same workload again. If the call graph becomes coherent, the frames were the problem rather than the profiler. ## A practical rule - Build profiling and production configurations with `-O2 -g -fno-omit-frame-pointer`, so a profile taken today matches what customers run. - Remember that debug information does not fix frame-pointer stacks; the register must be kept. - Check the dependencies: rebuild the important ones the same way, or accept gaps in their frames.
- Does compiling with -g fix broken frame-pointer stacks?No. `-g` adds DWARF that a DWARF-based unwinder can use, but a frame-pointer walk needs the register to actually hold the chain at run time. Only `-fno-omit-frame-pointer` restores that; debug information and frame pointers solve different halves of the problem.
- Why can a frame-pointer profile still lose frames inside a system library?The walk follows saved frame pointers from frame to frame. When execution passes through code compiled without frame pointers, that code uses the register for data, so the chain breaks there and every caller above it is lost or misattributed.
- -fomit-frame-pointer is only enabled at -O3.
- -fno-omit-frame-pointer guarantees a frame pointer in every function.
- Adding -g restores correct frame-pointer stacks.
- Keeping frame pointers halves the program's speed.
- Frame pointers are required for C++ exceptions to unwind.