Skip to content

Commit af57b2c

Browse files
committed
[Backtracing] Tweaks after Mike's remarks.
Just use `UInt` for `Address`. This is still the subject of some discussion on the forums, but I haven't decided precisely what to do about it and `UInt` makes sense for now. This also necessitated some casts elsewhere. Improve some comments. Made `limit` robust against silly negative values. A couple of formatting fixes. Don't bother supporting the macOS 10.12.2 SDK as Xcode now supports a minimum of 10.13.
1 parent 1c3b8b5 commit af57b2c

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

stdlib/public/Backtracing/Backtrace.swift

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@ import Swift
2727
public struct Backtrace: CustomStringConvertible, Sendable {
2828
/// The type of an address.
2929
///
30-
/// This will be `UInt32` or `UInt64` on current platforms.
31-
#if arch(x86_64) || arch(arm64)
32-
public typealias Address = UInt64
33-
#elseif arch(i386) || arch(arm)
34-
public typealias Address = UInt32
35-
#else
36-
#error("You need to fill this in for your architecture.")
37-
#endif
30+
/// This is intentionally _not_ a pointer, because you shouldn't be
31+
/// dereferencing them; they may refer to some other process, for
32+
/// example.
33+
public typealias Address = UInt
3834

3935
/// The unwind algorithm to use.
4036
public enum UnwindAlgorithm {
@@ -55,20 +51,27 @@ public struct Backtrace: CustomStringConvertible, Sendable {
5551

5652
/// Represents an individual frame in a backtrace.
5753
public enum Frame: CustomStringConvertible, Sendable {
58-
/// An accurate program counter.
54+
/// A program counter value.
5955
///
6056
/// This might come from a signal handler, or an exception or some
6157
/// other situation in which we have captured the actual program counter.
58+
///
59+
/// These can be directly symbolicated, as-is, with no adjustment.
6260
case programCounter(Address)
6361

6462
/// A return address.
6563
///
6664
/// Corresponds to a normal function call.
65+
///
66+
/// Requires adjustment when symbolicating for a backtrace, because it
67+
/// points at the address after the one that triggered the child frame.
6768
case returnAddress(Address)
6869

6970
/// An async resume point.
7071
///
7172
/// Corresponds to an `await` in an async task.
73+
///
74+
/// Can be directly symbolicated, as-is.
7275
case asyncResumePoint(Address)
7376

7477
/// Indicates a discontinuity in the backtrace.
@@ -174,7 +177,7 @@ public struct Backtrace: CustomStringConvertible, Sendable {
174177
///
175178
/// Some backtracing algorithms may require this information, in which case
176179
/// it will be filled in by the `capture()` method. Other algorithms may
177-
/// not, in which case it will be empty and you can capture an image list
180+
/// not, in which case it will be `nil` and you can capture an image list
178181
/// separately yourself using `captureImages()`.
179182
public var images: [Image]?
180183

@@ -249,7 +252,7 @@ public struct Backtrace: CustomStringConvertible, Sendable {
249252
.dropFirst(offset)
250253

251254
if let limit = limit {
252-
if limit == 0 {
255+
if limit <= 0 {
253256
return Backtrace(frames: [.truncated])
254257
}
255258

@@ -356,7 +359,7 @@ public struct Backtrace: CustomStringConvertible, Sendable {
356359
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
357360
let task = process as! task_t
358361

359-
withDyldProcessInfo(for: task){ dyldInfo in
362+
withDyldProcessInfo(for: task) { dyldInfo in
360363
_dyld_process_info_for_each_image(dyldInfo) {
361364
(machHeaderAddress, uuid, path) in
362365

@@ -375,7 +378,7 @@ public struct Backtrace: CustomStringConvertible, Sendable {
375378
// Find the end of the __TEXT segment
376379
var endOfText = machHeaderAddress + 4096
377380

378-
_dyld_process_info_for_each_segment(dyldInfo, machHeaderAddress){
381+
_dyld_process_info_for_each_segment(dyldInfo, machHeaderAddress) {
379382
address, size, name in
380383

381384
if let name = String(validatingUTF8: name!), name == "__TEXT" {
@@ -386,8 +389,8 @@ public struct Backtrace: CustomStringConvertible, Sendable {
386389
images.append(Image(name: name,
387390
path: pathString,
388391
buildID: theUUID,
389-
baseAddress: machHeaderAddress,
390-
endOfText: endOfText))
392+
baseAddress: Address(machHeaderAddress),
393+
endOfText: Address(endOfText)))
391394
}
392395
}
393396
}
@@ -411,15 +414,15 @@ public struct Backtrace: CustomStringConvertible, Sendable {
411414
public static func captureSharedCacheInfo(for t: Any) -> SharedCacheInfo? {
412415
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
413416
let task = t as! task_t
414-
return withDyldProcessInfo(for: task){ dyldInfo in
417+
return withDyldProcessInfo(for: task) { dyldInfo in
415418
var cacheInfo = dyld_process_cache_info()
416419
_dyld_process_info_get_cache(dyldInfo, &cacheInfo)
417-
let theUUID = withUnsafePointer(to: cacheInfo.cacheUUID){
420+
let theUUID = withUnsafePointer(to: cacheInfo.cacheUUID) {
418421
Array(UnsafeRawBufferPointer(start: $0,
419422
count: MemoryLayout<uuid_t>.size))
420423
}
421424
return SharedCacheInfo(uuid: theUUID,
422-
baseAddress: cacheInfo.cacheBaseAddress,
425+
baseAddress: Address(cacheInfo.cacheBaseAddress),
423426
noCache: cacheInfo.noCache)
424427
}
425428
#else // !os(Darwin)

stdlib/public/Backtracing/BacktraceFormatter.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,7 @@ private func measure(_ ch: Unicode.Scalar) -> Int {
294294
return 1
295295
}
296296

297-
if #available(macOS 10.12.2, *) {
298-
if ch.properties.isEmoji {
299-
return 2
300-
}
301-
} else if ch.value >= 0x1f100 && ch.value <= 0x1fb00 {
297+
if ch.properties.isEmoji {
302298
return 2
303299
}
304300

stdlib/public/Backtracing/SymbolicatedBacktrace.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,15 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
262262
fn: (CSSymbolicatorRef) throws -> T) rethrows -> T {
263263
let binaryImageList = images.map{ image in
264264
BinaryImageInformation(
265-
base: image.baseAddress,
266-
extent: image.endOfText,
265+
base: mach_vm_address_t(image.baseAddress),
266+
extent: mach_vm_address_t(image.endOfText),
267267
uuid: uuidBytesFromBuildID(image.buildID!),
268268
arch: HostContext.coreSymbolicationArchitecture,
269269
path: image.path,
270270
relocations: [
271271
BinaryRelocationInformation(
272-
base: image.baseAddress,
273-
extent: image.endOfText,
272+
base: mach_vm_address_t(image.baseAddress),
273+
extent: mach_vm_address_t(image.endOfText),
274274
name: "__TEXT"
275275
)
276276
],
@@ -333,7 +333,7 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
333333
let theSymbol = Symbol(imageIndex: imageIndex,
334334
imageName: imageName,
335335
rawName: rawName,
336-
offset: Int(address - range.location),
336+
offset: Int(address - UInt(range.location)),
337337
sourceLocation: location)
338338
theSymbol.name = name
339339

@@ -376,7 +376,7 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
376376
case .omittedFrames(_), .truncated:
377377
frames.append(Frame(captured: frame, symbol: nil))
378378
default:
379-
let address = frame.adjustedProgramCounter
379+
let address = mach_vm_address_t(frame.adjustedProgramCounter)
380380
let owner
381381
= CSSymbolicatorGetSymbolOwnerWithAddressAtTime(symbolicator,
382382
address,
@@ -390,7 +390,7 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
390390
let pos = frames.count
391391
var first = true
392392

393-
_ = CSSymbolOwnerForEachStackFrameAtAddress(owner, address){
393+
_ = CSSymbolOwnerForEachStackFrameAtAddress(owner, address) {
394394
symbol, sourceInfo in
395395

396396
frames.insert(buildFrame(from: frame,

0 commit comments

Comments
 (0)