Skip to content

Commit e0cdd7f

Browse files
committed
Reduce the number of captured symbols in backtraces.
1 parent 8e121e4 commit e0cdd7f

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

Sources/Testing/SourceAttribution/Backtrace.swift

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,36 @@ public struct Backtrace: Sendable {
6161
public static func current(maximumAddressCount addressCount: Int = 128) -> Self {
6262
// NOTE: the exact argument/return types for backtrace() vary across
6363
// platforms, hence the use of .init() when calling it below.
64-
let addresses = [UnsafeRawPointer?](unsafeUninitializedCapacity: addressCount) { addresses, initializedCount in
65-
addresses.withMemoryRebound(to: UnsafeMutableRawPointer?.self) { addresses in
64+
let addresses = [UnsafeMutableRawPointer?](unsafeUninitializedCapacity: addressCount) { addresses, initializedCount in
6665
#if SWT_TARGET_OS_APPLE
67-
if #available(_backtraceAsyncAPI, *) {
68-
initializedCount = backtrace_async(addresses.baseAddress!, addresses.count, nil)
69-
} else {
70-
initializedCount = .init(backtrace(addresses.baseAddress!, .init(addresses.count)))
71-
}
72-
#elseif os(Linux)
66+
if #available(_backtraceAsyncAPI, *) {
67+
initializedCount = backtrace_async(addresses.baseAddress!, addresses.count, nil)
68+
} else {
7369
initializedCount = .init(backtrace(addresses.baseAddress!, .init(addresses.count)))
70+
}
7471
#elseif os(Android)
75-
addresses.withMemoryRebound(to: UnsafeMutableRawPointer.self) { addresses in
76-
initializedCount = .init(backtrace(addresses.baseAddress!, .init(addresses.count)))
77-
}
72+
initializedCount = addresses.withMemoryRebound(to: UnsafeMutableRawPointer.self) { addresses in
73+
.init(backtrace(addresses.baseAddress!, .init(addresses.count)))
74+
}
75+
#elseif os(Linux)
76+
initializedCount = .init(backtrace(addresses.baseAddress!, .init(addresses.count)))
7877
#elseif os(Windows)
79-
initializedCount = Int(RtlCaptureStackBackTrace(0, ULONG(addresses.count), addresses.baseAddress!, nil))
78+
initializedCount = Int(RtlCaptureStackBackTrace(0, ULONG(addresses.count), addresses.baseAddress!, nil))
8079
#elseif os(WASI)
81-
// SEE: https://github.com/WebAssembly/WASI/issues/159
82-
// SEE: https://github.com/swiftlang/swift/pull/31693
83-
initializedCount = 0
80+
// SEE: https://github.com/WebAssembly/WASI/issues/159
81+
// SEE: https://github.com/swiftlang/swift/pull/31693
82+
initializedCount = 0
8483
#else
8584
#warning("Platform-specific implementation missing: backtraces unavailable")
86-
initializedCount = 0
85+
initializedCount = 0
8786
#endif
87+
}
88+
89+
return addresses.withUnsafeBufferPointer { addresses in
90+
addresses.withMemoryRebound(to: UnsafeRawPointer?.self) { addresses in
91+
Self(addresses: addresses)
8892
}
8993
}
90-
return Self(addresses: addresses)
9194
}
9295
}
9396

@@ -164,12 +167,12 @@ extension Backtrace {
164167
/// - errorAddress: The error that is about to be thrown. This pointer
165168
/// refers to an instance of `SwiftError` or (on platforms with
166169
/// Objective-C interop) an instance of `NSError`.
167-
@Sendable private static func _willThrow(_ errorAddress: UnsafeMutableRawPointer) {
170+
/// - backtrace: The backtrace from where the error was thrown.
171+
private static func _willThrow(_ errorAddress: UnsafeMutableRawPointer, from backtrace: Backtrace) {
168172
_oldWillThrowHandler.rawValue?(errorAddress)
169173

170174
let errorObject = unsafeBitCast(errorAddress, to: (any AnyObject & Sendable).self)
171175
let errorID = ObjectIdentifier(errorObject)
172-
let backtrace = Backtrace.current()
173176
let newEntry = _ErrorMappingCacheEntry(errorObject: errorObject, backtrace: backtrace)
174177

175178
_errorMappingCache.withLock { cache in
@@ -185,7 +188,10 @@ extension Backtrace {
185188
/// only once.
186189
private static let _startCachingForThrownErrors: Void = {
187190
_oldWillThrowHandler.withLock { oldWillThrowHandler in
188-
oldWillThrowHandler = swt_setWillThrowHandler { _willThrow($0) }
191+
oldWillThrowHandler = swt_setWillThrowHandler { errorAddress in
192+
let backtrace = Backtrace.current()
193+
_willThrow(errorAddress, from: backtrace)
194+
}
189195
}
190196
}()
191197

0 commit comments

Comments
 (0)