Skip to content

Commit b3a5cb7

Browse files
committed
Separate raw metadata and generic metadata dump commands
1 parent 23ebf78 commit b3a5cb7

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

tools/swift-inspect/Sources/swift-inspect/Metadata.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,23 @@ extension Allocation {
4242
}
4343
}
4444

45+
extension BidirectionalCollection where Element == Allocation {
46+
func findGenericMetadata(in context: SwiftReflectionContextRef) -> [Metadata] {
47+
var metadatas = self.compactMap { $0.metadata(in: context) }
48+
for i in metadatas.indices {
49+
let metadata = metadatas[i]
50+
if let allocation = self.last(where: { metadata.ptr >= $0.ptr }) {
51+
metadatas[i].allocation = allocation
52+
}
53+
}
54+
return metadatas
55+
}
56+
}
57+
4558
struct Metadata {
4659
let ptr: swift_reflection_ptr_t
60+
var allocation: Allocation? = nil
4761
let name: String
62+
63+
var offset: Int? { allocation.map { Int(self.ptr - $0.ptr) } }
4864
}

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,29 @@ func dumpConformanceCache(context: SwiftReflectionContextRef) throws {
3333
}
3434
}
3535

36-
func dumpMetadataAllocations(context: SwiftReflectionContextRef) throws {
37-
var allocations = context.allocations
38-
for allocation in allocations {
36+
func dumpRawMetadata(context: SwiftReflectionContextRef) throws {
37+
for allocation in context.allocations {
3938
print("Metadata allocation at: \(hex: allocation.ptr) " +
4039
"size: \(allocation.size) tag: \(allocation.tag)")
4140
}
42-
43-
allocations.sort()
44-
let metadatas = allocations.compactMap { $0.metadata(in: context) }
41+
}
42+
43+
func dumpGenericMetadata(context: SwiftReflectionContextRef) throws {
44+
let allocations = context.allocations.sorted()
45+
let metadatas = allocations.findGenericMetadata(in: context)
4546

47+
print("Address","Allocation","Size","Offset","Name", separator: "\t")
4648
for metadata in metadatas {
47-
print("Metadata \(hex: metadata.ptr)")
48-
print(" Name: \(metadata.name)")
49+
print("\(hex: metadata.ptr)", terminator: "\t")
4950

50-
if let allocation = allocations.last(where: { metadata.ptr >= $0.ptr }) {
51-
let offset = metadata.ptr - allocation.ptr
52-
print(" In allocation \(hex: allocation.ptr) " +
53-
"size \(allocation.size) at offset \(offset)")
51+
if let allocation = metadata.allocation, let offset = metadata.offset {
52+
print("\(hex: allocation.ptr)\t\(allocation.size)\t\(offset)",
53+
terminator: "\t")
5454
} else {
55-
print(" Not in any known metadata allocation. How strange.")
55+
print("???\t???\t???", terminator: "\t")
56+
5657
}
58+
print(metadata.name)
5759
}
5860
}
5961

@@ -99,7 +101,8 @@ struct SwiftInspect: ParsableCommand {
99101
abstract: "Swift runtime debug tool",
100102
subcommands: [
101103
DumpConformanceCache.self,
102-
DumpMetadataAllocations.self,
104+
DumpRawMetadata.self,
105+
DumpGenericMetadata.self,
103106
])
104107
}
105108

@@ -117,7 +120,21 @@ struct DumpConformanceCache: ParsableCommand {
117120
}
118121
}
119122

120-
struct DumpMetadataAllocations: ParsableCommand {
123+
struct DumpRawMetadata: ParsableCommand {
124+
static let configuration = CommandConfiguration(
125+
abstract: "Print the target's metadata allocations.")
126+
@Argument(help: "The pid or partial name of the target process")
127+
128+
var nameOrPid: String
129+
130+
func run() throws {
131+
try withReflectionContext(nameOrPid: nameOrPid) {
132+
try dumpRawMetadata(context: $0)
133+
}
134+
}
135+
}
136+
137+
struct DumpGenericMetadata: ParsableCommand {
121138
static let configuration = CommandConfiguration(
122139
abstract: "Print the target's metadata allocations.")
123140
@Argument(help: "The pid or partial name of the target process")
@@ -126,7 +143,7 @@ struct DumpMetadataAllocations: ParsableCommand {
126143

127144
func run() throws {
128145
try withReflectionContext(nameOrPid: nameOrPid) {
129-
try dumpMetadataAllocations(context: $0)
146+
try dumpGenericMetadata(context: $0)
130147
}
131148
}
132149
}

0 commit comments

Comments
 (0)