Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
16 changes: 15 additions & 1 deletion Sources/Backtrace/Backtrace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Sources/Sample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import Darwin
import Glibc
#endif

#if swift(<5.9) || os(Windows)
Backtrace.install()
#endif

func raiseSignal(_ signal: Int32) {
raise(signal)
Expand Down
20 changes: 20 additions & 0 deletions Tests/BacktraceTests/BacktraceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down