-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The swift-backtrace library is no longer needed in 5.9 (#3064)
* The swift-backtrace library is no longer needed in 5.9 * Deprecate StackTrace and immediately remove all its functionality. * Clear up a whole pile of stray print()s in the tests (they'll run faster and actually be testing something)
- Loading branch information
Showing
20 changed files
with
53 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,37 @@ | ||
import Foundation | ||
#if os(Linux) | ||
import Backtrace | ||
import CBacktrace | ||
#endif | ||
import NIOConcurrencyHelpers | ||
|
||
@available(*, deprecated, message: "Captured stack traces are no longer supported by Vapor") | ||
extension Optional where Wrapped == StackTrace { | ||
public static func capture(skip: Int = 0) -> Self { | ||
StackTrace.capture(skip: 1 + skip) | ||
} | ||
public static func capture(skip: Int = 0) -> Self { StackTrace() } | ||
} | ||
|
||
@available(*, deprecated, message: "Captured stack traces are no longer supported by Vapor") | ||
public struct StackTrace: Sendable { | ||
public static var isCaptureEnabled: Bool { | ||
get { | ||
_isCaptureEnabled.withLockedValue { | ||
$0 | ||
} | ||
} | ||
set { | ||
_isCaptureEnabled.withLockedValue { | ||
$0 = newValue | ||
} | ||
} | ||
} | ||
private static let _isCaptureEnabled: NIOLockedValueBox<Bool> = .init(true) | ||
|
||
public static func capture(skip: Int = 0) -> Self? { | ||
guard Self.isCaptureEnabled else { | ||
return nil | ||
} | ||
let frames = Self.captureRaw().dropFirst(1 + skip) | ||
return .init(rawFrames: .init(frames)) | ||
} | ||
|
||
#if os(Linux) | ||
private static let state = backtrace_create_state(CommandLine.arguments[0], /* supportThreading: */ 1, nil, nil) | ||
#endif | ||
|
||
static func captureRaw() -> [RawFrame] { | ||
#if os(Linux) | ||
final class Context { | ||
var frames: [RawFrame] = [] | ||
} | ||
let context = Context() | ||
backtrace_full(self.state, /* skip: */ 1, { data, pc, filename, lineno, function in | ||
let frame = RawFrame( | ||
file: filename.flatMap { String(cString: $0) } ?? "unknown", | ||
mangledFunction: function.flatMap { String(cString: $0) } ?? "unknown" | ||
) | ||
Unmanaged<Context>.fromOpaque(data!).takeUnretainedValue().frames.append(frame) | ||
return 0 | ||
}, { _, cMessage, _ in | ||
let message = cMessage.flatMap { String(cString: $0) } ?? "unknown" | ||
fatalError("Failed to capture Linux stacktrace: \(message)") | ||
}, Unmanaged.passUnretained(context).toOpaque()) | ||
return context.frames | ||
#else | ||
return Thread.callStackSymbols.dropFirst(1).map { line in | ||
let parts = line.split( | ||
separator: " ", | ||
maxSplits: 3, | ||
omittingEmptySubsequences: true | ||
) | ||
let file = parts.count > 1 ? String(parts[1]) : "unknown" | ||
let functionPart = parts.count > 3 ? (parts[3].split(separator: "+").first.map({ String($0) }) ?? "unknown") : "unknown" | ||
let mangledFunction = functionPart.trimmingCharacters(in: .whitespaces) | ||
return .init(file: file, mangledFunction: mangledFunction) | ||
} | ||
#endif | ||
} | ||
|
||
public struct Frame: Sendable { | ||
public var file: String | ||
public var function: String | ||
} | ||
|
||
public var frames: [Frame] { | ||
self.rawFrames.map { frame in | ||
Frame( | ||
file: frame.file, | ||
function: _stdlib_demangleName(frame.mangledFunction) | ||
) | ||
} | ||
} | ||
|
||
struct RawFrame: Sendable { | ||
var file: String | ||
var mangledFunction: String | ||
} | ||
|
||
let rawFrames: [RawFrame] | ||
|
||
public func description(max: Int = 16) -> String { | ||
return self.frames[..<min(self.frames.count, max)].readable | ||
public static var isCaptureEnabled: Bool { | ||
get { false } | ||
set { } | ||
} | ||
public static func capture(skip: Int = 0) -> Self? { nil } | ||
public var frames: [Frame] { [] } | ||
public func description(max: Int = 16) -> String { "" } | ||
} | ||
|
||
@available(*, deprecated, message: "Captured stack traces are no longer supported by Vapor") | ||
extension StackTrace: CustomStringConvertible { | ||
public var description: String { | ||
self.description() | ||
} | ||
public var description: String { self.description() } | ||
} | ||
|
||
@available(*, deprecated, message: "Captured stack traces are no longer supported by Vapor") | ||
extension StackTrace.Frame: CustomStringConvertible { | ||
public var description: String { | ||
"\(self.file) \(self.function)" | ||
} | ||
public var description: String { "\(self.file) \(self.function)" } | ||
} | ||
|
||
@available(*, deprecated, message: "Captured stack traces are no longer supported by Vapor") | ||
extension Collection where Element == StackTrace.Frame, Index: BinaryInteger { | ||
var readable: String { | ||
let maxIndexWidth = self.indices.max(by: { String($0).count < String($1).count }).map { String($0).count } ?? 0 | ||
let maxFileWidth = self.max(by: { $0.file.count < $1.file.count })?.file.count ?? 0 | ||
return self.enumerated().map { i, frame in | ||
let indexPad = String(repeating: " ", count: Swift.max(0, maxIndexWidth - String(i).count)) | ||
let filePad = String(repeating: " ", count: Swift.max(0, maxFileWidth - frame.file.count)) | ||
|
||
return "\(i)\(indexPad) \(frame.file)\(filePad) \(frame.function)" | ||
}.joined(separator: "\n") | ||
} | ||
var readable: String { "" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.