Skip to content

Commit f085f2b

Browse files
committed
swift-inspect: fix all the typos/correctness issues
Fix the many typos and missing `)` instances. Replace the inline array removal with explicit duplication due to the behaviour of `#if`. This allows the tool to build after the changes for the refactoring.
1 parent 4453571 commit f085f2b

File tree

9 files changed

+57
-43
lines changed

9 files changed

+57
-43
lines changed

tools/swift-inspect/Sources/swift-inspect/Backtrace.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ internal func backtrace(_ stack: [swift_reflection_ptr_t], style: BacktraceStyle
2828
// shallowest, so `main` will be somewhere near the end.
2929
switch style {
3030
case .oneline:
31-
return self.reversed().map { entry($0) }.joined(separator: " | ")
31+
return stack.reversed().map { entry($0) }.joined(separator: " | ")
3232
case .long:
33-
return self.reversed().enumerated().map {
34-
" \(String(repeating: " ", count: $0 + 1)\(entry($1))"
35-
}.joined(separtor: "\n")
33+
return stack.reversed().enumerated().map {
34+
" \(String(repeating: " ", count: $0 + 1))\(entry($1))"
35+
}.joined(separator: "\n")
3636
}
3737
}

tools/swift-inspect/Sources/swift-inspect/DarwinRemoteProcess.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal final class DarwinRemoteProcess: RemoteProcess {
2424
public var process: ProcessHandle { task }
2525
public private(set) var context: SwiftReflectionContextRef!
2626

27-
private var swiftCore: ??? = ???
27+
private var swiftCore: CSTypeRef
2828

2929
static var QueryDataLayout: QueryDataLayoutFunction {
3030
return { (context, type, _, output) in
@@ -43,7 +43,7 @@ internal final class DarwinRemoteProcess: RemoteProcess {
4343

4444
case DLQ_GetObjCReservedLowBits:
4545
var size: UInt8 = 0
46-
#if os(macoS)
46+
#if os(macOS)
4747
// Only 64-bit macOS reserves pointer bit-packing.
4848
if MemoryLayout<UnsafeRawPointer>.stride == 8 { size = 1 }
4949
#endif
@@ -102,7 +102,7 @@ internal final class DarwinRemoteProcess: RemoteProcess {
102102
var task: task_t = task_t()
103103
let result = task_for_pid(mach_task_self_, processId, &task)
104104
guard result == KERN_SUCCESS else {
105-
print("unable to get task for pid \(processId): \(String(cString: mach_error_string(result)) (0x\(String(result, radix: 16)))")
105+
print("unable to get task for pid \(processId): \(String(cString: mach_error_string(result))) (0x\(String(result, radix: 16)))")
106106
return nil
107107
}
108108
self.task = task
@@ -154,7 +154,7 @@ extension DarwinRemoteProcess {
154154
ranges.forEach {
155155
body(swift_addr_t($0.address), UInt64($0.size))
156156
}
157-
}
157+
})
158158
}
159159
}
160160
}
@@ -165,7 +165,7 @@ extension DarwinRemoteProcess {
165165

166166
let result = task_threads(self.task, &threadList, &threadCount)
167167
guard result == KERN_SUCCESS else {
168-
print("unable to gather threads for process: \(String(cString: mach_error_string(result)) (0x\(String(result, radix: 16)))")
168+
print("unable to gather threads for process: \(String(cString: mach_error_string(result))) (0x\(String(result, radix: 16)))")
169169
return []
170170
}
171171

@@ -195,7 +195,7 @@ extension DarwinRemoteProcess {
195195
thread_info(threadList![i], thread_flavor_t(THREAD_IDENTIFIER_INFO),
196196
$0, &infoCount)
197197
guard result == ERROR_SUCCESS else {
198-
print("unable to get info for thread \(i): \(String(cString: mach_error_string(result)) (0x\(String(result, radix: 16)))")
198+
print("unable to get info for thread \(i): \(String(cString: mach_error_string(result))) (0x\(String(result, radix: 16)))")
199199
return
200200
}
201201

tools/swift-inspect/Sources/swift-inspect/Operations/DumpConformanceCache.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal struct DumpConformanceCache: ParsableCommand {
2020
var options: UniversalOptions
2121

2222
func run() throws {
23-
try inspec(process: options.nameOrPid) { process in
23+
try inspect(process: options.nameOrPid) { process in
2424
try process.context.iterateConformanceCache { type, proto in
2525
let type: String = process.context.name(type: type) ?? "<unknown>"
2626
let conformance: String = process.context.name(protocol: proto) ?? "<unknown>"

tools/swift-inspect/Sources/swift-inspect/Operations/DumpGenericMetadata.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private struct Metadata {
1818
var allocation: swift_metadata_allocation_t?
1919

2020
let name: String
21-
let isArayOfClass: Bool
21+
let isArrayOfClass: Bool
2222

2323
var offset: Int? { allocation.map { Int(self.ptr - $0.ptr) } }
2424
}
@@ -28,7 +28,7 @@ internal struct DumpGenericMetadata: ParsableCommand {
2828
abstract: "Print the target's generic metadata allocations.")
2929

3030
@OptionGroup()
31-
var universalOptions: UniversalOptions
31+
var options: UniversalOptions
3232

3333
@OptionGroup()
3434
var backtraceOptions: BacktraceOptions
@@ -50,7 +50,9 @@ internal struct DumpGenericMetadata: ParsableCommand {
5050
}
5151

5252
let stacks: [swift_reflection_ptr_t:[swift_reflection_ptr_t]]? =
53-
backtrace.style == nil ? nil : try process.context.allocationStacks
53+
backtraceOptions.style == nil
54+
? nil
55+
: try process.context.allocationStacks
5456

5557
print("Address", "Allocation", "Size", "Offset", "isArrayOfClass", "Name", separator: "\t")
5658
generics.forEach {
@@ -62,7 +64,7 @@ internal struct DumpGenericMetadata: ParsableCommand {
6264
}
6365
print($0.isArrayOfClass, terminator: "\t")
6466
print($0.name)
65-
if let style = backtrace.style, let allocation = $0.allocation {
67+
if let style = backtraceOptions.style, let allocation = $0.allocation {
6668
if let stack = stacks?[allocation.ptr] {
6769
print(backtrace(stack, style: style, process.symbolicate))
6870
} else {

tools/swift-inspect/Sources/swift-inspect/Operations/DumpRawMetadata.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ internal struct DumpRawMetadata: ParsableCommand {
1818
abstract: "Print the target's metadata allocations.")
1919

2020
@OptionGroup()
21-
var universalOptions: UniversalOptions
21+
var options: UniversalOptions
2222

2323
@OptionGroup()
2424
var backtraceOptions: BacktraceOptions
2525

2626
func run() throws {
2727
try inspect(process: options.nameOrPid) { process in
2828
let stacks: [swift_reflection_ptr_t:[swift_reflection_ptr_t]]? =
29-
backtrace.style == nil ? nl : try process.context.allocationStacks
29+
backtraceOptions.style == nil
30+
? nil
31+
: try process.context.allocationStacks
3032

3133
try process.context.allocations.forEach { allocation in
3234
let name: String = process.context.name(allocation: allocation.tag) ?? "<unknown>"
33-
print("Metadata allocation at: \(hex: allocation.ptr) size: \(allocation.size) tag: \(allocation.tag) (\(name))"
34-
if let style = backtrace.style {
35+
print("Metadata allocation at: \(hex: allocation.ptr) size: \(allocation.size) tag: \(allocation.tag) (\(name))")
36+
if let style = backtraceOptions.style {
3537
if let stack = stacks?[allocation.ptr] {
3638
print(backtrace(stack, style: style, process.symbolicate))
3739
} else {

tools/swift-inspect/Sources/swift-inspect/Process.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212

1313
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
1414
import Darwin
15-
#endif
1615

17-
internal typealias ProcessIdentifier = pid_t
16+
internal typealias ProcessIdentifier = DarwinRemoteProcess.ProcessIdentifier
1817

1918
internal func process(matching: String) -> ProcessIdentifier? {
20-
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
2119
return pidFromHint(matching)
22-
#endif
2320
}
21+
#endif

tools/swift-inspect/Sources/swift-inspect/RemoteMirror+Extensions.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension swift_metadata_allocation_t: Comparable {
2323
lhs.ptr == rhs.ptr
2424
}
2525

26-
public static func < (lhs: self, rhs: Self) -> Bool {
26+
public static func < (lhs: Self, rhs: Self) -> Bool {
2727
lhs.ptr < rhs.ptr
2828
}
2929
}
@@ -46,7 +46,7 @@ extension SwiftReflectionContextRef {
4646
internal var allocations: [swift_metadata_allocation_t] {
4747
get throws {
4848
var allocations: [swift_metadata_allocation_t] = []
49-
try iterateMetdataAllocations {
49+
try iterateMetadataAllocations {
5050
allocations.append($0)
5151
}
5252
return allocations
@@ -56,18 +56,19 @@ extension SwiftReflectionContextRef {
5656
internal var allocationStacks: [swift_reflection_ptr_t:[swift_reflection_ptr_t]] {
5757
get throws {
5858
var stacks: [swift_reflection_ptr_t:[swift_reflection_ptr_t]] = [:]
59-
try iterateMetadataAllocationBacktraces { allocation, count, stack, in
60-
stacks[allocation] = Aray(UnsafeBufferPointer(start: stack, count: count))
59+
try iterateMetadataAllocationBacktraces { allocation, count, stack in
60+
stacks[allocation] =
61+
Array(UnsafeBufferPointer(start: stack, count: count))
6162
}
6263
return stacks
6364
}
6465
}
6566

6667
internal func name(type: swift_reflection_ptr_t) -> String? {
67-
let typeref: UInt64 = swift_reflection_typeRefForMetadata(self, UInt(type)
68+
let typeref: UInt64 = swift_reflection_typeRefForMetadata(self, UInt(type))
6869
if typeref == 0 { return nil }
6970

70-
guard let name = swift_reflection_copyDemangled_NameForTypeRef(self, typeref) else {
71+
guard let name = swift_reflection_copyDemangledNameForTypeRef(self, typeref) else {
7172
return nil
7273
}
7374
defer { free(name) }

tools/swift-inspect/Sources/swift-inspect/RemoteProcess.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal protocol RemoteProcess: AnyObject {
1616
associatedtype ProcessIdentifier
1717
associatedtype ProcessHandle
1818

19-
var process: ProcesHandle { get }
19+
var process: ProcessHandle { get }
2020
var context: SwiftReflectionContextRef! { get }
2121

2222
init?(processId: ProcessIdentifier)

tools/swift-inspect/Sources/swift-inspect/main.swift

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ internal struct BacktraceOptions: ParsableArguments {
2828

2929
var style: BacktraceStyle? {
3030
if backtraceLong { return .long }
31-
if backtrace { return .oneLine }
31+
if backtrace { return .oneline }
3232
return nil
3333
}
3434
}
3535

3636

37-
internal func inspect<Process: RemoteProcess>(process pattern: String,
38-
_ body: (Process) throws -> Void) throws {
37+
internal func inspect(process pattern: String,
38+
_ body: (any RemoteProcess) throws -> Void) throws {
3939
guard let processId = process(matching: pattern) else {
4040
print("No process found matching \(pattern)")
4141
return
@@ -53,16 +53,27 @@ internal func inspect<Process: RemoteProcess>(process pattern: String,
5353

5454
@main
5555
internal struct SwiftInspect: ParsableCommand {
56-
static let configuration = CommandConfiguration(
57-
abstract: "Swift runtime debug tool",
58-
subcommands: [
59-
DumpConformanceCache.self,
60-
DumpRawMetadata.self,
61-
DumpGenericMetadata.self,
62-
DumpCacheNodes.self,
56+
// DumpArrays and DumpConcurrency cannot be reliably be ported outside of
57+
// Darwin due to the need to iterate the heap.
6358
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
64-
DumpArrays.self,
65-
DumpConcurrency.self,
59+
static let subcommands: [ParsableCommand.Type] = [
60+
DumpConformanceCache.self,
61+
DumpRawMetadata.self,
62+
DumpGenericMetadata.self,
63+
DumpCacheNodes.self,
64+
DumpArrays.self,
65+
DumpConcurrency.self,
66+
]
67+
#else
68+
static let subcommands: [ParsableCommand.Type] = [
69+
DumpConformanceCache.self,
70+
DumpRawMetadata.self,
71+
DumpGenericMetadata.self,
72+
DumpCacheNodes.self,
73+
]
6674
#endif
67-
])
75+
76+
static let configuration = CommandConfiguration(
77+
abstract: "Swift runtime debug tool",
78+
subcommands: subcommands)
6879
}

0 commit comments

Comments
 (0)