This is an archive of the discontinued LLVM Phabricator instance.

X86: In debug build, emit CFI data in X86FrameLowering and X86CallFrameOptimization
Needs ReviewPublic

Authored by alexinbeijing on May 31 2015, 12:03 AM.
This revision needs review, but there are no reviewers specified.

Details

Reviewers
None
Summary

Both X86FrameLowering and X86CallFrameOptimization emit PUSHes/POPs and ADDs/SUBs to the stack pointer. If each of these instructions is not accompanied by an appropriate CFI instruction, then when single-stepping through the resulting binaries in a debugger, backtraces will be unavailable at many points. Actually, it's worse than that; sometimes the debugger will show a wildly incorrect backtrace.

There are still places where more work is needed on CFI generation, but this seems to cover the most important ones. At least, I can single-step through a test program and get a correct backtrace at every point.

Note that some optimizations which merge consecutive instructions may need to become smarter to perform merging when a CFI instruction is in the middle.

I'm not happy with the duplication of the (rather verbose) code to check whether generation of Dwarf data was requested or not; it seems that this should just be done once per build, or once per compilation unit, and the results used everywhere else.

Diff Detail

Event Timeline

alexinbeijing retitled this revision from to X86: In debug build, emit CFI data in X86FrameLowering and X86CallFrameOptimization.
alexinbeijing updated this object.
alexinbeijing edited the test plan for this revision. (Show Details)
alexinbeijing changed the edit policy from "All Users" to "Administrators".
alexinbeijing added a subscriber: Unknown Object (MLST).
NOTE: I have found a couple of bugs here. I will post an updated revision soon.
alexinbeijing edited the test plan for this revision. (Show Details)May 31 2015, 2:35 AM

A few tweaks and bug fixes here.

Note that this breaks a couple regression tests which depend on the exact name of emitted temp labels. (It emits more temp labels of its own.)

asl added a subscriber: asl.Jun 5 2015, 2:58 AM

Please update all these tests to pass, surely.

rnk added a subscriber: rnk.Jun 8 2015, 1:30 PM

Thanks for looking into this, I agree this is important functionality. I also agree this code is fairly duplicated and needs cleanup.

I have a feeling that emitting asynch unwind tables by default is going to break some tools that consume our unwind tables. I believe ld64 processes our .eh_frame data to produce it's own compact CFI format, and it may choke on these kinds of cfa updates. We're going to need a way to turn off these fine-grained updates if something breaks. The simplest way to flag this would be to add a bool to TargetOptions and default it to true. If it causes someone problems, we can flip the default (or tweak it based on platform) instead of reverting the patch. Ultimately, we can wire this up to -fasynchronous-unwind-tables in clang, and (hopefully) keep that on by default.

This also needs much more comprehensive testing, new tests in addition to the ones already present.

lib/Target/X86/X86FrameLowering.cpp
226–231

I won't ask you to refactor this goo, I'll see if I can do it myself at some point.

258–261

Can you maybe wrap this all up in a static helper like BuildAdjustCFAMI? Anything would be good.

285–286

This doesn't appear to handle the sub case?