pprof-rs: Panic on aarch64
tikv/tikv#10658
TiKV on HUAWEI,Kunpeng 920 failed to profile and got an panic.
#0 0x0000fffd7b6aceb4 in ?? () from /lib64/libgcc_s.so.1
#1 0x0000fffd7b6ae534 in _Unwind_Backtrace () from /lib64/libgcc_s.so.1
#2 0x0000aaac01eedb58 in backtrace::backtrace::libunwind::trace (cb=...) at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.37/src/backtrace/libunwind.rs:88
#3 backtrace::backtrace::trace_unsynchronized (cb=...) at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.37/src/backtrace/mod.rs:66
#4 pprof::profiler::perf_signal_handler (_signal=<optimized out>) at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/pprof-0.4.2/src/profiler.rs:128
#5 <signal handler called>
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 46 (37 by maintainers)
There is some confusion in various comments on this issue so I’ll try to clear up some of it.
The difference between stack walking and symbolication, and how it relates to inline functions
The resolution of inline functions happens during symbolication: A single address can resolve to one or more functions. If the address is inside code which the compiler inlined into another function, then you get both the inlined function name (or even multiple inline function names, if the compiler inlined multiple levels deep), and then the outer function name. Whether you get inline functions is completely independent from how you walk the stack.
If you want to do stack walking “offline”, you need to capture the entire stack bytes. If you want to do inline function resolution offline, you only need to capture the code addresses on the stack (instruction pointer + return addresses) and enough information to be able to match an address to the library it was in.
A slightly confusing part here is that both stack walking and symbolication can make use of DWARF information. However, they’re different subsets of DWARF information. DWARF stack walking information is stored in the
eh_frame
ordebug_frame
sections. DWARF symbolication information is stored in a other sections which start withdebug_
, but not indebug_frame
.Unwinding / stack walking on macOS
On macOS x86_64 and arm64, all system libraries are compiled with frame pointers enabled. And having framepointers enabled is also the default for clang and Xcode, unless you manually set -fomit-frame-pointer. So frame pointer stack walking mostly works fine, unless you’re profiling a program that has been compiled with -fomit-frame-pointer. There is one exception: On arm64, leaf functions don’t have frame pointers even if you compile with frame pointers enabled. This means that on arm64, if you just use frame pointer unwinding, you will be missing the second frame in the stack if you’re currently inside the leaf function: The first frame will be correct (it comes straight from the instruction pointer), the immediate caller is missing, and the frame pointer gives you the caller’s caller, i.e. the third frame. From that point on the rest of stack unwinding works fine.
To unwind leaf functions correctly, you need to look at the compact unwind info in the
__unwind_info
section.Unwind information sections
On macOS, most binaries will have an
__unwind_info
section, and some will also have an__eh_frame
section. For complete unwinding, you need both:__unwind_info
covers the simple cases, and__eh_frame
covers the hard cases.Here’s how it breaks down.
On x86_64:
__unwind_info
covers 99% of the functions.__eh_frame
is needed to correctly unwind the remaining 1% of cases.On arm64:
__unwind_info
lets you unwind leaf functions correctly, but still requires frame pointers for non-leaf functions.__eh_frame
lets you unwind if you don’t have frame pointers.I’ve written a crate called
framehop
which is a pure Rust implementation of everything you need to get reliable and correct unwinding on macOS.It doesn’t have much documentation yet though.Edit: Documentation is in place now.The
nongnu/libunwind
announces that it supports unwind from a signal handler (and accept an argument to tell it whether we are in a signal handler), though we haven’t fully tested it yet.It’s always glad to see another implementation of
libunwind
. Hope it can be better than any existing one (as we have suffered a lot with these 😭 )Thanks for @SchrodingerZhu . I have tried to use nongnu libunwind and the
unw_xxx
API to get the backtrace in arm. It works surprisingly well!The modification on
backtrace-rs
can be found in https://github.com/YangKeao/backtrace-rs/commit/ee71341ca6a1ea68e2c60677a4d9b31f0042378b@sticnarf @mornyx Building
nongnu-libunwind
withoutenable-cxx-exceptions
will not build the function_Unwind_XXX
(e.g._Unwind_Backtrace
), which is also the default behavior of the nongnu-libunwind shipped by ubuntu. I think it’s fine to directly static-link with it.The libunwind-sys needs to be modified to support static link. I will try to modify it and submit a PR.
I did a test and it does exactly what @mstange says, here is a simple demo:
On my macOS, the ARM64 asm code generated by clang is as follows:
Assuming we are executing
func1
at this time, when we start backtracking directly infunc1
, the starting point is the IP register in the scope offunc1
. But sincefunc1
does not save the FP register (x29), the Frame Pointer still points to the frame start address offunc2
, and the frame start address offunc2
stores the return address within the range offunc3
. So fromfunc1
will directly backtrack tofunc3
.But in a regular backtracking scenario, this problem can be easily avoided. We usually wrap the stack backtrace as a function like
backtrace()
, and in the implementation ofbacktrace()
, we call a function likegetcontext()
to initialize the register context. So withgetcontext()
as a leaf function, we skip thebacktrace()
function when backtracking, which is exactly what we want.The only thing that needs to be done is to ensure that the
backtrace()
andgetcontext()
functions are not inlined.The rust demo below proves this conclusion:
The output is (on my ARM64 macOS):
This is exactly what we expected.
However, when the scene comes to CPU Profiling, if the leaf function is interrupted by the
SIGPROF
, I think its parent function will indeed be skipped. This can be avoided by setting-mno-omit-leaf-frame-pointer
.Just to clarify: I am talking about the subset of functions which, on arm64, will not create a “frame record” for themselves even if you compile with frame pointers enabled. This happens for functions which do not call other functions (i.e. which are “leaf” functions) and which also don’t need to save and restore any registers.
These functions leave x29 (“fp”) unchanged. And because they don’t call any other functions, the lr register also stays unchanged. So unwinding from these functions only means “get the return address from lr and leave all other registers unchanged”.
If it did that then yes, frame pointer unwinding would not work at all and there would be no way to get the rest of the stack. You would need to use some kind of unwind information to recover a usable frame pointer value. But luckily these functions leave the frame pointer from the parent frame intact.
Yes, in code compiled with -fomit-frame-pointer, the easy solution fails. Luckily macOS has established a culture of always enabling frame pointers.
Great, please file issues if you run into any trouble. Framehop only solves a subset of the problem; it’s still up to the user to find where libraries are mapped in memory, to get their unwind section data, and to read the stack memory in a way that doesn’t cause segfaults. Framehop is mostly about speed, caching, handling multiple types of unwind data, and supporting the offline use case on different machines and architectures.
Don’t worry. We have testing environments to run different complicated user payloads. It can be pretty helpful to discover problems and make it close to “battle-tested”.
Right~ This also seems to be possible by modifying
nongnu/libunwind
(by convertingucontext
tounw_context_t
instead of callingunw_getcontext()
in the signal handler).But this only solved half the problem for us (as you said, and also doesn’t support macOS). So I reimplemented a minimal subset of libunwind, to try to avoid the problems you said.
I tested it on newer versions of macOS, and in fact the binary only contains the
__unwind_info
segment, no longer the__eh_frame
segment. The point is that__unwind_info
does not contain inline information either. Howeverbacktrace::resolve()
can correctly call callback for inlined functions.The demo below confirms this point:
output:
Looking at
0x100a82fd4
, we got two functions for one pointer, including the one which is inlined.