9 Commits

Author SHA1 Message Date
tomer doron baf9597d12 trap SIGSEGV, SIGBUS and SIGFPE by default, in addition to SIGILL (#58)
motivation: print crash trace on segmentation faults and other common failures beyond illegal instructions

changes:
* include SIGSEGV, SIGBUS and SIGFPE in the default signals trapped
* update and improve tests
2022-06-08 09:06:33 -07:00
Konrad `ktoso` Malawski 7ba397e50d Merge pull request #57 from Fabio1988/patch-1
exit after printing stacktrace #51
2022-04-06 10:45:57 +09:00
Fabio1988 ad485ae151 exit after printing stacktrace #51 2022-03-17 12:17:02 +01:00
tomer doron 662e436989 ci update (#56)
motivation: 5.6 is out

changes:
* use release version of 5.6
* add docker setup for 5.7 (using nightly for now)
2022-03-16 21:15:06 -07:00
Konrad `ktoso` Malawski 923624a26e Merge pull request #53 from swift-server/tomerd-patch-2
update CI setup with new versions of swift
2021-09-23 06:03:22 +09:00
tomer doron e2621e2fa1 Create docker-compose.2004.main.yaml
* add ci setup for nightly toolchain
2021-09-22 12:02:54 -07:00
tomer doron ea4824d488 Update Dockerfile
address jazzy install issues on older ubuntu versions
2021-09-22 12:00:46 -07:00
tomer doron f1db8bf93d update CI setup with new versions of swift
motivation: 5.5 release is available

changes: 
* add docker CI setup to for 5.5
2021-09-22 11:56:43 -07:00
tomer doron 99a6c79fc5 Update README.md (#50)
motivation: highlight the background for this library and incourage use via service-lifecycle 

changes: update readme to reflect these ideas
2021-07-28 16:13:56 -07:00
11 changed files with 169 additions and 21 deletions
+6
View File
@@ -2,8 +2,14 @@
This Swift package provides support for automatically printing crash backtraces of Swift programs.
The library is designed to fill a gap in backtraces support for Swift on non-Darwin platforms.
When this gap is closed at the language runtime level, this library will become redundant and be deprecated.
## 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.
Add `https://github.com/swift-server/swift-backtrace.git` as a dependency in your `Package.swift`.
### Crash backtraces
+3 -2
View File
@@ -67,9 +67,9 @@ private func printBacktrace(signal: CInt) {
}
public enum Backtrace {
/// Install the backtrace handler on `SIGILL`.
/// Install the backtrace handler on default signals: `SIGILL`, `SIGSEGV`, `SIGBUS`, `SIGFPE`.
public static func install() {
Backtrace.install(signals: [SIGILL])
Backtrace.install(signals: [SIGILL, SIGSEGV, SIGBUS, SIGFPE])
}
/// Install the backtrace handler when any of `signals` happen.
@@ -77,6 +77,7 @@ public enum Backtrace {
for signal in signals {
self.setupHandler(signal: signal) { signal in
printBacktrace(signal: signal)
raise(signal)
}
}
}
+23 -2
View File
@@ -13,7 +13,28 @@
//===----------------------------------------------------------------------===//
import Backtrace
#if canImport(Darwin)
import Darwin
#elseif os(Linux)
import Glibc
#endif
Backtrace.install()
func raiseSignal(_ signal: Int32) {
raise(signal)
}
let reason = CommandLine.arguments.count == 2 ? CommandLine.arguments[1] : "unknown"
Backtrace.install()
fatalError(reason)
switch reason.uppercased() {
case "SIGILL":
raiseSignal(SIGILL)
case "SIGSEGV":
raiseSignal(SIGSEGV)
case "SIGBUS":
raiseSignal(SIGBUS)
case "SIGFPE":
raiseSignal(SIGFPE)
default:
fatalError(reason)
}
@@ -25,7 +25,11 @@ import XCTest
extension BacktraceTests {
public static var allTests: [(String, (BacktraceTests) -> () throws -> Void)] {
return [
("testBacktrace", testBacktrace),
("testFatalError", testFatalError),
("testSIGILL", testSIGILL),
("testSIGSEGV", testSIGSEGV),
("testSIGBUS", testSIGBUS),
("testSIGFPE", testSIGFPE),
]
}
}
+66 -12
View File
@@ -15,22 +15,76 @@
import XCTest
public final class BacktraceTests: XCTestCase {
func testBacktrace() {
#if os(Linux)
func testFatalError() throws {
#if !os(Linux)
try XCTSkipIf(true, "test is only supported on Linux")
#endif
let expectedError = UUID().uuidString
let stderr = try runSample(reason: expectedError)
print(stderr)
XCTAssert(stderr.contains("Received signal 4. Backtrace:"))
XCTAssert(stderr.contains("Current stack trace:"), "expected stanard error to include backtrace")
XCTAssert(stderr.contains("Fatal error: \(expectedError)"), "expected stanard error to include error information")
}
func testSIGILL() throws {
#if !os(Linux)
try XCTSkipIf(true, "test is only supported on Linux")
#endif
let stderr = try runSample(reason: "SIGILL")
print(stderr)
XCTAssert(stderr.contains("Received signal \(SIGILL). Backtrace:"))
XCTAssert(stderr.contains("Sample.raiseSignal"))
}
func testSIGSEGV() throws {
#if !os(Linux)
try XCTSkipIf(true, "test is only supported on Linux")
#endif
let stderr = try runSample(reason: "SIGSEGV")
print(stderr)
XCTAssert(stderr.contains("Received signal \(SIGSEGV). Backtrace:"))
XCTAssert(stderr.contains("Sample.raiseSignal"))
}
func testSIGBUS() throws {
#if !os(Linux)
try XCTSkipIf(true, "test is only supported on Linux")
#endif
let stderr = try runSample(reason: "SIGBUS")
print(stderr)
XCTAssert(stderr.contains("Received signal \(SIGBUS). Backtrace:"))
XCTAssert(stderr.contains("Sample.raiseSignal"))
}
func testSIGFPE() throws {
#if !os(Linux)
try XCTSkipIf(true, "test is only supported on Linux")
#endif
let stderr = try runSample(reason: "SIGFPE")
print(stderr)
XCTAssert(stderr.contains("Received signal \(SIGFPE). Backtrace:"))
XCTAssert(stderr.contains("Sample.raiseSignal"))
}
func runSample(reason: String) throws -> String {
let pipe = Pipe()
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/swift")
process.arguments = ["run", "Sample", expectedError]
process.arguments = ["run", "Sample", reason]
process.standardError = pipe
XCTAssertNoThrow(try process.run())
if process.isRunning {
process.waitUntilExit()
}
let stderr = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
print(stderr)
XCTAssert(stderr.contains("Current stack trace:"), "expected stanard error to include backtrace")
XCTAssert(stderr.contains("Fatal error: \(expectedError)"), "expected stanard error to include error information")
#endif
try process.run()
process.waitUntilExit()
return String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
}
}
+3 -3
View File
@@ -18,9 +18,9 @@ RUN apt-get update && apt-get install -y lsof dnsutils netcat-openbsd net-tools
# ruby and jazzy for docs generation
RUN apt-get update && apt-get install -y ruby ruby-dev libsqlite3-dev build-essential
# switch of gem docs building
RUN echo "gem: --no-document" > ~/.gemrc
RUN if [ "${ubuntu_version}" != "xenial" ] ; then gem install jazzy ; fi
# jazzy no longer works on older version of ubuntu as ruby is too old.
RUN if [ "${ubuntu_version}" = "focal" ] ; then echo "gem: --no-document" > ~/.gemrc ; fi
RUN if [ "${ubuntu_version}" = "focal" ] ; then gem install jazzy ; fi
# tools
RUN mkdir -p $HOME/.tools
+16
View File
@@ -0,0 +1,16 @@
version: "3"
services:
runtime-setup:
image: swift-linux-backtrace:20.04-5.5
build:
args:
ubuntu_version: "focal"
swift_version: "5.5"
test:
image: swift-linux-backtrace:20.04-5.5
shell:
image: swift-linux-backtrace:20.04-5.5
+16
View File
@@ -0,0 +1,16 @@
version: "3"
services:
runtime-setup:
image: swift-linux-backtrace:20.04-5.6
build:
args:
ubuntu_version: "focal"
swift_version: "5.6"
test:
image: swift-linux-backtrace:20.04-5.6
shell:
image: swift-linux-backtrace:20.04-5.6
+15
View File
@@ -0,0 +1,15 @@
version: "3"
services:
runtime-setup:
image: swift-linux-backtrace:20.04-5.7
build:
args:
base_image: "swiftlang/swift:nightly-main-focal"
test:
image: swift-linux-backtrace:20.04-5.7
shell:
image: swift-linux-backtrace:20.04-5.7
+15
View File
@@ -0,0 +1,15 @@
version: "3"
services:
runtime-setup:
image: swift-linux-backtrace:20.04-main
build:
args:
base_image: "swiftlang/swift:nightly-main-focal"
test:
image: swift-linux-backtrace:20.04-main
shell:
image: swift-linux-backtrace:20.04-main
+1 -1
View File
@@ -34,4 +34,4 @@ services:
shell:
<<: *common
entrypoint: /bin/bash
entrypoint: /bin/bash -l