Skip to content

Commit a6c9ca2

Browse files
committed
Reduce the number of captured symbols in backtraces.
1 parent 133e302 commit a6c9ca2

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

Sources/Testing/SourceAttribution/Backtrace.swift

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,32 @@ 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+
}
71+
#elseif os(Linux)
72+
initializedCount = .init(backtrace(addresses.baseAddress!, .init(addresses.count)))
7473
#elseif os(Windows)
75-
initializedCount = Int(RtlCaptureStackBackTrace(0, ULONG(addresses.count), addresses.baseAddress!, nil))
74+
initializedCount = Int(RtlCaptureStackBackTrace(0, ULONG(addresses.count), addresses.baseAddress!, nil))
7675
#elseif os(WASI)
77-
// SEE: https://github.com/WebAssembly/WASI/issues/159
78-
// SEE: https://github.com/swiftlang/swift/pull/31693
79-
initializedCount = 0
76+
// SEE: https://github.com/WebAssembly/WASI/issues/159
77+
// SEE: https://github.com/swiftlang/swift/pull/31693
78+
initializedCount = 0
8079
#else
8180
#warning("Platform-specific implementation missing: backtraces unavailable")
82-
initializedCount = 0
81+
initializedCount = 0
8382
#endif
83+
}
84+
85+
return addresses.withUnsafeBufferPointer { addresses in
86+
addresses.withMemoryRebound(to: UnsafeRawPointer?.self) { addresses in
87+
Self(addresses: addresses)
8488
}
8589
}
86-
return Self(addresses: addresses)
8790
}
8891
}
8992

@@ -160,12 +163,12 @@ extension Backtrace {
160163
/// - errorAddress: The error that is about to be thrown. This pointer
161164
/// refers to an instance of `SwiftError` or (on platforms with
162165
/// Objective-C interop) an instance of `NSError`.
163-
@Sendable private static func _willThrow(_ errorAddress: UnsafeMutableRawPointer) {
166+
/// - backtrace: The backtrace from where the error was thrown.
167+
private static func _willThrow(_ errorAddress: UnsafeMutableRawPointer, from backtrace: Backtrace) {
164168
_oldWillThrowHandler.rawValue?(errorAddress)
165169

166170
let errorObject = unsafeBitCast(errorAddress, to: (any AnyObject & Sendable).self)
167171
let errorID = ObjectIdentifier(errorObject)
168-
let backtrace = Backtrace.current()
169172
let newEntry = _ErrorMappingCacheEntry(errorObject: errorObject, backtrace: backtrace)
170173

171174
_errorMappingCache.withLock { cache in
@@ -181,7 +184,10 @@ extension Backtrace {
181184
/// only once.
182185
private static let _startCachingForThrownErrors: Void = {
183186
_oldWillThrowHandler.withLock { oldWillThrowHandler in
184-
oldWillThrowHandler = swt_setWillThrowHandler { _willThrow($0) }
187+
oldWillThrowHandler = swt_setWillThrowHandler { errorAddress in
188+
let backtrace = Backtrace.current()
189+
_willThrow(errorAddress, from: backtrace)
190+
}
185191
}
186192
}()
187193

0 commit comments

Comments
 (0)