Skip to content

Commit 1c3b8b5

Browse files
committed
[Backtracing] Various improvements following Johannes' remarks.
Removed some unnecessary memory rebinding. Made `CFString` conversion slightly more efficient. Provide the `SharedCacheInfo` fields everywhere, but make it optional all over as well. rdar://104336548
1 parent 4edfacb commit 1c3b8b5

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

stdlib/public/Backtracing/Backtrace.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ public struct Backtrace: CustomStringConvertible, Sendable {
180180

181181
/// Holds information about the shared cache.
182182
public struct SharedCacheInfo: Sendable {
183-
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
184183
/// The UUID from the shared cache.
185184
public var uuid: [UInt8]
186185

@@ -189,7 +188,6 @@ public struct Backtrace: CustomStringConvertible, Sendable {
189188

190189
/// Says whether there is in fact a shared cache.
191190
public var noCache: Bool
192-
#endif
193191
}
194192

195193
/// Information about the shared cache.
@@ -401,34 +399,31 @@ public struct Backtrace: CustomStringConvertible, Sendable {
401399
/// Capture shared cache information.
402400
///
403401
/// @returns A `SharedCacheInfo`.
404-
public static func captureSharedCacheInfo() -> SharedCacheInfo {
402+
public static func captureSharedCacheInfo() -> SharedCacheInfo? {
405403
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
406404
return captureSharedCacheInfo(for: mach_task_self_)
407405
#else
408-
return SharedCacheInfo()
406+
return nil
409407
#endif
410408
}
411409

412410
@_spi(Internal)
413-
public static func captureSharedCacheInfo(for t: Any) -> SharedCacheInfo {
411+
public static func captureSharedCacheInfo(for t: Any) -> SharedCacheInfo? {
414412
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
415413
let task = t as! task_t
416414
return withDyldProcessInfo(for: task){ dyldInfo in
417415
var cacheInfo = dyld_process_cache_info()
418416
_dyld_process_info_get_cache(dyldInfo, &cacheInfo)
419417
let theUUID = withUnsafePointer(to: cacheInfo.cacheUUID){
420-
$0.withMemoryRebound(to: UInt8.self,
421-
capacity: MemoryLayout<uuid_t>.size) {
422-
Array(UnsafeBufferPointer(start: $0,
423-
count: MemoryLayout<uuid_t>.size))
424-
}
418+
Array(UnsafeRawBufferPointer(start: $0,
419+
count: MemoryLayout<uuid_t>.size))
425420
}
426421
return SharedCacheInfo(uuid: theUUID,
427422
baseAddress: cacheInfo.cacheBaseAddress,
428423
noCache: cacheInfo.noCache)
429424
}
430425
#else // !os(Darwin)
431-
return SharedCacheInfo()
426+
return nil
432427
#endif
433428
}
434429

stdlib/public/Backtracing/BacktraceFormatter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ public struct BacktraceFormatter {
690690

691691
while feof(fp) == 0 && ferror(fp) == 0 {
692692
guard let result = fgets(buffer.baseAddress,
693-
Int32(buffer.count), fp) else {
693+
CInt(buffer.count), fp) else {
694694
break
695695
}
696696

stdlib/public/Backtracing/CoreSymbolication.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ private enum Sym {
117117
// CFString. As a result, we need to do the dance manually.
118118

119119
private func toCFString(_ s: String) -> CFString! {
120-
let bytes = Array<UInt8>(s.utf8)
121-
return bytes.withUnsafeBufferPointer{
120+
var s = s
121+
return s.withUTF8 {
122122
return CFStringCreateWithBytes(nil,
123123
$0.baseAddress,
124124
$0.count,
@@ -135,10 +135,8 @@ private func fromCFString(_ cf: CFString) -> String {
135135

136136
if let ptr = CFStringGetCStringPtr(cf,
137137
CFStringBuiltInEncodings.ASCII.rawValue) {
138-
return ptr.withMemoryRebound(to: UInt8.self, capacity: length) {
139-
return String(decoding: UnsafeBufferPointer(start: $0, count: length),
140-
as: UTF8.self)
141-
}
138+
return String(decoding: UnsafeRawBufferPointer(start: ptr, count: length),
139+
as: UTF8.self)
142140
} else {
143141
var byteLen = CFIndex(0)
144142

stdlib/public/Backtracing/SymbolicatedBacktrace.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
211211
public var images: [Backtrace.Image]
212212

213213
/// Shared cache information.
214-
public var sharedCacheInfo: Backtrace.SharedCacheInfo
214+
public var sharedCacheInfo: Backtrace.SharedCacheInfo?
215215

216216
/// True if this backtrace is a Swift runtime failure.
217217
public var isSwiftRuntimeFailure: Bool {
@@ -233,7 +233,7 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
233233

234234
/// Construct a SymbolicatedBacktrace from a backtrace and a list of images.
235235
private init(backtrace: Backtrace, images: [Backtrace.Image],
236-
sharedCacheInfo: Backtrace.SharedCacheInfo,
236+
sharedCacheInfo: Backtrace.SharedCacheInfo?,
237237
frames: [Frame]) {
238238
self.backtrace = backtrace
239239
self.images = images
@@ -258,7 +258,7 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
258258

259259
/// Create a symbolicator.
260260
private static func withSymbolicator<T>(images: [Backtrace.Image],
261-
sharedCacheInfo: Backtrace.SharedCacheInfo,
261+
sharedCacheInfo: Backtrace.SharedCacheInfo?,
262262
fn: (CSSymbolicatorRef) throws -> T) rethrows -> T {
263263
let binaryImageList = images.map{ image in
264264
BinaryImageInformation(
@@ -357,7 +357,7 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
357357
theImages = Backtrace.captureImages()
358358
}
359359

360-
let theCacheInfo: Backtrace.SharedCacheInfo
360+
let theCacheInfo: Backtrace.SharedCacheInfo?
361361
if let sharedCacheInfo = sharedCacheInfo {
362362
theCacheInfo = sharedCacheInfo
363363
} else if let sharedCacheInfo = backtrace.sharedCacheInfo {
@@ -450,14 +450,14 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
450450
lines.append("\(n)\t\(image)")
451451
}
452452

453-
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
454-
lines.append("")
455-
lines.append("Shared Cache:")
456-
lines.append("")
457-
lines.append(" UUID: \(hex(sharedCacheInfo.uuid))")
458-
lines.append(" Base: \(hex(sharedCacheInfo.baseAddress))")
459-
lines.append(" Active: \(!sharedCacheInfo.noCache)")
460-
#endif
453+
if let sharedCacheInfo = sharedCacheInfo {
454+
lines.append("")
455+
lines.append("Shared Cache:")
456+
lines.append("")
457+
lines.append(" UUID: \(hex(sharedCacheInfo.uuid))")
458+
lines.append(" Base: \(hex(sharedCacheInfo.baseAddress))")
459+
lines.append(" Active: \(!sharedCacheInfo.noCache)")
460+
}
461461

462462
return lines.joined(separator: "\n")
463463
}

0 commit comments

Comments
 (0)