Before we have an issue with artificial LBR whose source is a return, recalling that "an internal code(A) can return to external address, then from the external address call a new internal code(B), making an artificial branch that looks like a return from A to B can confuse the unwinder". We just ignore the LBRs after this artificial LBR which can miss some samples. This change aims at fixing this by correctly unwinding them instead of ignoring them.
List some typical scenarios covered by this change.
- multiple sequential call back happen in external address, e.g.
[ext, call, foo] [foo, return, ext] [ext, call, bar]
Unwinder should avoid having foo return from bar. Wrong call stack is like [foo, bar]
- the call stack before and after external call should be correctly unwinded.
{call stack1} {call stack2} [foo, call, ext] [ext, call, bar] [bar, return, ext] [ext, return, foo ]
call stack 1 should be the same to call stack2. Both shouldn't be truncated
- call stack should be truncated after call into external code since we can't do inlining with external code.
[foo, call, ext] [ext, call, bar] [bar, call, baz] [baz, return, bar ] [bar, return, ext]
the call stack of code in baz should not include foo.
Implementation:
We leverage artificial frame to fix #2 and #3: when we got a return artificial LBR, push an extra artificial frame to the stack. when we pop frame, check if the parent is an artificial frame to pop(fix #2). Therefore, call/ return artificial LBR is just the same as regular LBR which can keep the call stack.
While recording context on the trie, artificial frame is used as a tag indicating that we should truncate the call stack(fix #3).
To differentiate #1 and #2, we leverage getCallAddrFromFrameAddr. Normally the target of the return should be the next inst of a call inst and getCallAddrFromFrameAddr will return the address of call inst. Otherwise, getCallAddrFromFrameAddr will return to 0 which is the case of #1.
Comment for the definition and purpose for artificial frame.