From 1d503b4c57d9ed61d960b5fd9289d1d24ca642f8 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Thu, 22 Jun 2023 16:10:44 +0100 Subject: [PATCH] Disable this library for Swift 5.9 and above. Swift 5.9 has a new built-in backtrace-on-crash facility, which supersedes this library. rdar://111109405 --- README.md | 4 ++-- Sources/Backtrace/Backtrace.swift | 16 +++++++++++++++- Sources/Sample/main.swift | 2 ++ Tests/BacktraceTests/BacktraceTests.swift | 20 ++++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6b8e2a2..92dd633 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ When this gap is closed at the language runtime level, this library will become ## Usage -When building web-services and daemons, direct usage of this library is discouraged. -Instead, use [swift-service-lifecycle](https://github.com/swift-server/swift-service-lifecycle) which helps manage the application lifecycle including setting up backtraces hooks when needed. +**Note**: You do not need this library on Linux as of Swift 5.9, which has +built-in backtracing support. Add `https://github.com/swift-server/swift-backtrace.git` as a dependency in your `Package.swift`. diff --git a/Sources/Backtrace/Backtrace.swift b/Sources/Backtrace/Backtrace.swift index 792eb3d..b69b450 100644 --- a/Sources/Backtrace/Backtrace.swift +++ b/Sources/Backtrace/Backtrace.swift @@ -12,7 +12,21 @@ // //===----------------------------------------------------------------------===// -#if os(Linux) +// Swift 5.9 has its own built-in backtracing support in the runtime; +// we don't want to activate this library if we're using 5.9 or above. +#if swift(>=5.9) && !os(Windows) +public enum Backtrace { + @available(*, deprecated, message: "This is no longer needed in Swift 5.9") + public static func install() {} + + @available(*, deprecated, message: "This is no longer needed in Swift 5.9") + public static func install(signals: [CInt]) {} + + @available(*, deprecated, message: "This method will be removed in the next major version.") + public static func print() {} +} + +#elseif os(Linux) import CBacktrace import Glibc diff --git a/Sources/Sample/main.swift b/Sources/Sample/main.swift index b6a0b39..994bf55 100644 --- a/Sources/Sample/main.swift +++ b/Sources/Sample/main.swift @@ -19,7 +19,9 @@ import Darwin import Glibc #endif +#if swift(<5.9) || os(Windows) Backtrace.install() +#endif func raiseSignal(_ signal: Int32) { raise(signal) diff --git a/Tests/BacktraceTests/BacktraceTests.swift b/Tests/BacktraceTests/BacktraceTests.swift index dbe3f16..bb7f471 100644 --- a/Tests/BacktraceTests/BacktraceTests.swift +++ b/Tests/BacktraceTests/BacktraceTests.swift @@ -20,6 +20,10 @@ public final class BacktraceTests: XCTestCase { try XCTSkipIf(true, "test is only supported on Linux") #endif + #if swift(>=5.9) + try XCTSkipIf(true, "test is not supported on Swift 5.9") + #endif + let expectedError = UUID().uuidString let stderr = try runSample(reason: expectedError) print(stderr) @@ -34,6 +38,10 @@ public final class BacktraceTests: XCTestCase { try XCTSkipIf(true, "test is only supported on Linux") #endif + #if swift(>=5.9) + try XCTSkipIf(true, "test is not supported on Swift 5.9") + #endif + let stderr = try runSample(reason: "SIGILL") print(stderr) @@ -46,6 +54,10 @@ public final class BacktraceTests: XCTestCase { try XCTSkipIf(true, "test is only supported on Linux") #endif + #if swift(>=5.9) + try XCTSkipIf(true, "test is not supported on Swift 5.9") + #endif + let stderr = try runSample(reason: "SIGSEGV") print(stderr) @@ -58,6 +70,10 @@ public final class BacktraceTests: XCTestCase { try XCTSkipIf(true, "test is only supported on Linux") #endif + #if swift(>=5.9) + try XCTSkipIf(true, "test is not supported on Swift 5.9") + #endif + let stderr = try runSample(reason: "SIGBUS") print(stderr) @@ -70,6 +86,10 @@ public final class BacktraceTests: XCTestCase { try XCTSkipIf(true, "test is only supported on Linux") #endif + #if swift(>=5.9) + try XCTSkipIf(true, "test is not supported on Swift 5.9") + #endif + let stderr = try runSample(reason: "SIGFPE") print(stderr)