Skip to content

[Backtracing] Add improved backtracing support for Swift crashes #64100

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

Merged
merged 28 commits into from
Mar 4, 2023

Conversation

al45tair
Copy link
Contributor

@al45tair al45tair commented Mar 4, 2023

This PR adds on-crash backtracing for Swift programs on macOS.

al45tair added 28 commits March 4, 2023 07:56
When a Swift program crashes, we should catch the crash and execute the
external backtracer.

rdar://105391747
In order for the runtime PR to work as a separate PR, it does need a little
bit of code in the `_SwiftBacktracing.h` header in SwiftShims.

rdar://105391747
We really, really shouldn't be running the external backtracer for setuid
binaries.  It's just too dangerous.

So don't do that.  And if someone tries to force us, emit a warning.

rdar://105391747
If we find signal handlers already installed, leave them alone.

rdar://105391747
Need to change this to `swift_copyAuxiliaryExecutablePath()`.

rdar://105391747
Adds a new swift_Backtracing library, with a corresponding _Backtracing
module, to the build.  Also add some tests.

This is not public API at this point, but will be used by the external
backtracing program, `swift-backtrace`.

rdar://104336548
We need to enable MASM/MARMASM and we need to add a get-cpu-context.asm
file for it to build.

rdar://104336548
The correct name for the runtime library appears to be CRT, not MSVCRT.

rdar://104336548
Some of the `TARGET` and `os()` conditionals needed to be updated.

rdar://104336548
Linux doesn't have `issetugid()`, so use `getauxval(AT_SECURE)` there
instead.

rdar://105391747
Accidentally wrote `thread_resume()` instead of `thread_suspend()`.

rdar://105391747
Removed some unnecessary memory rebinding.

Made `CFString` conversion slightly more efficient.

Provide the `SharedCacheInfo` fields everywhere, but make it optional all
over as well.

rdar://104336548
Just use `UInt` for `Address`.  This is still the subject of some discussion
on the forums, but I haven't decided precisely what to do about it and `UInt`
makes sense for now.  This also necessitated some casts elsewhere.

Improve some comments.

Made `limit` robust against silly negative values.

A couple of formatting fixes.

Don't bother supporting the macOS 10.12.2 SDK as Xcode now supports a minimum
of 10.13.
iOS doesn't have <libproc.h>.  We don't need it anyway there.

rdar://104336548
Once the API has gone through Swift Evolution, we will want to implicitly
import the _Backtracing module.  Add code to do that, but set it to off
by default for now.

rdar://105394140
This is Swift's external backtracer.  It's written in Swift, and it's
responsible for generating nice backtraces when Swift programs crash.
It makes use of the new `_Backtracing` library, which is also (mostly,
aside from some assembly language) implemented in Swift.

rdar://103442000
Fixed the colours so that they work with all of the default Terminal
presets.  Also changed things so that when colour is off, we only use
ASCII characters in our source code displays.

rdar://105452194
The name of the C library on Windows is CRT, apparently.

rdar://105452194
…cer.

Add some discussion of how the new external backtracer works and what
options are available.

rdar://105394365
…cOS.

Added a list of handled signals and some notes on what the runtime will
do if it finds signal handlers already configured.

rdar://105394365
Some symbolication frameworks have a symbol cache; we probably don't want
to use that for test cases, to avoid running into problems where the cache
holds stale information.

rdar://105409147
While I was doing this, it turns out Saleem was fixing things to avoid
having to patch the Windows include directories, which is awesome but
necessitates an extra change to the backtracing stuff to make the build
not fail on Windows.

rdar://105409147
The Linux Swift compiler is ICEing when building _Backtracing, while
Windows still needs some tweaks.  In order to land this sooner, disable
this code except for macOS.
There was a missing "e" in "movl (%esp), %edx".
Added some extra code to AddSwiftStdlib.cmake so executable targets can
specify target SDKs the same way libraries currently can.

Updated the Backtracing targets to specify just OS X for now.
Implicit imports were off for library builds already, but we need them off
for executable builds too, otherwise we have problems with _StringProcessing.
For some reason we don't see the dyld frames in CI.  That's fine, don't
check for them.

Also turn NotImportedByDefault off for Windows as it needs a better
shell than the support provided there.
@al45tair al45tair requested a review from xedin as a code owner March 4, 2023 08:02
@al45tair
Copy link
Contributor Author

al45tair commented Mar 4, 2023

@swift-ci Please test

@al45tair
Copy link
Contributor Author

al45tair commented Mar 4, 2023

The individual pieces of this have already been reviewed as a set of stacked PRs:

PR Title
al45tair#8 [Backtracing] Add support for looking up paths for auxiliary executables
al45tair#9 [Backtracing][Runtime][Darwin] Add code to trigger the external backtracer
al45tair#10 [Backtracing] Add _Backtracing library to the build
al45tair#11 [Backtracing][Frontend] Add support for implicit import of _Backtracing
al45tair#12 [Backtracing] Add swift-backtrace to the build
al45tair#13 [Backtracing][Docs] Add an explanation of the workings of the backtracer
al45tair#14 [Backtracing] Add control over symbol caching

@al45tair al45tair merged commit bc47698 into swiftlang:main Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant