Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backtracing][Linux] Add support for Linux in the _Backtracing module. #66408

Merged
merged 14 commits into from
Jun 7, 2023

Conversation

al45tair
Copy link
Contributor

@al45tair al45tair commented Jun 7, 2023

Rearrange things to avoid using SwiftShims, because that's grotty and means we have to carry all kinds of definitions from the C library. Instead, turn off implicit modules for the _Backtracing build and add some explicit modules that we can use as we please.

Also add the code to handle ELF and DWARF for Linux, including symbol lookup, demangling, line number/source file extraction, Linux mcontext_t support et al.

Tests for all of this will be in a separate PR that also enables everything.

rdar://110261712

This also adds a function to demangle a symbol, and a way for the
backtracing code to report warning messages to the same place as
the main runtime.

I'd like to rename the _swift_isThunkFunction() SPI also, but we
can't do that until we've made the changes to the _Backtracing
library, so we'll do that there instead.

rdar://110261430
This was added to a later PR, but not to this one, though we need
it here.

rdar://110261430
This should have been disabled until apple#66338.

rdar://110261430
Mike and Max made various helpful suggestions, so I've added and updated
various comments and amended the code to cope with partial reads and
writes.

rdar://110261430
Moved the comment for `_swift_backtrace_demangle` into the header file
instead of it being in the implementation.

rdar://110261430
The Swift backtracer's frame pointer unwinder cannot work on Linux
without this change, because the compiler omits the frame pointer from
the function in libSwift_Backtracing that actually captures the stack.

rdar://110260855
Using `SwiftShims` is undesirable - it creates all kinds of build issues,
and means shipping the `_SwiftBacktracing.h` header in the SDK, which is
not necessary.

While we're doing this, add the necessary definitions for reading ELF
and DWARF information.

rdar://110261712
Use the new module structure rather the old SwiftShims header.  This
is much cleaner and lets us include operating system headers to get
the relevant definitions where possible.

Add code to support ELF and DWARF, including decompression using
zlib, zstd and liblzma if those turn out to be required and available.

rdar://110261712
This is for compatibility, so that I can split up the PRs.
We'll remove it in the next PR.

rdar://110261712
There's a chance that pipes might perform a partial read; we should
handle that case.

rdar://110261712
The `status` argument to the `_swift_backtrace_demangle()` function
isn't especially useful, won't match behaviour on Windows, and we
actually don't use it in the Swift code that calls this SPI.

Remove it.

rdar://110261712
`__cxa_demangle()` is a rather unusual API; one of its "features" is that
the pointer you pass in must either be `nullptr`, in which case it
will call `malloc()` itself, _or_ it has to be a pointer to a block
of memory allocated with `malloc()`, because `__cxa_demangle()` may
`realloc()` it for you.

This seems to me to be something of a non-obvious footgun, so we never
pass the caller's pointer through to `__cxa_demangle()`, which lets them
decide how they want to allocate space.

rdar://110261712
…ule.

There's a separate declaration here because we can't include the `Backtrace.h`
header from inside `modules/Runtime/Runtime.h`.

rdar://110261712
The `_Backtracing` module has a number of private implementation only
imports that aren't used outside of the module and that don't require
any additional libraries (hence they aren't relevant to the outside
world).  `verify_all_overlays.py` needs to know about these when it
does its test, because it loadas the module as the main module, which
results in implementation only imports being required instead of
ignored.

rdar://110261712
@al45tair al45tair requested a review from a team as a code owner June 7, 2023 12:22
@al45tair al45tair added 🍒 release cherry pick Flag: Release branch cherry picks swift 5.9 labels Jun 7, 2023
@al45tair
Copy link
Contributor Author

al45tair commented Jun 7, 2023

This depends on #66407 and #66371 (the commits for which are also in this PR so that the build will work). 33e1af1 is the first commit that is really part of this PR.

@al45tair
Copy link
Contributor Author

al45tair commented Jun 7, 2023

Explanation: Adds Linux support into the _Backtracing module.
Risk: Low. Only affects Linux, and this code is disabled until the later enabling PR.
Original PR: #66335
Reviewed by: @mikeash @etcwilde
Resolves: rdar://110261712
Tests: The Linux unwinding tests exercise this code (but those are in the enabling PR as they won't work until this is enabled).

@al45tair
Copy link
Contributor Author

al45tair commented Jun 7, 2023

@swift-ci Please test

@al45tair al45tair merged commit 827b4fd into apple:release/5.9 Jun 7, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍒 release cherry pick Flag: Release branch cherry picks swift 5.9
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants