Skip to content

Commit f0c8640

Browse files
committed
Print whether the metadata is a _ContiguousArrayStorage with a class element type
1 parent 28b53de commit f0c8640

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ extension Allocation {
3535
let ptr = context.metadataPointer(allocation: allocation_t)
3636
if ptr != 0 {
3737
let name = context.name(metadata: ptr) ?? "<unknown>"
38-
return .init(ptr: ptr, name: name)
38+
let isClass = context.isAContiguousArrayOfClassElementType(metadata: ptr)
39+
return .init(ptr: ptr, name: name, isArrayOfClass: isClass)
3940
} else {
4041
return nil
4142
}
@@ -59,6 +60,7 @@ struct Metadata {
5960
let ptr: swift_reflection_ptr_t
6061
var allocation: Allocation? = nil
6162
let name: String
63+
let isArrayOfClass: Bool
6264

6365
var offset: Int? { allocation.map { Int(self.ptr - $0.ptr) } }
6466
}

tools/swift-inspect/Sources/swift-inspect/RemoteMirrorExtensions.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ extension SwiftReflectionContextRef {
3131
return String(cString: cstr)
3232
}
3333

34+
func isAContiguousArrayOfClassElementType(metadata: swift_reflection_ptr_t) ->
35+
Bool {
36+
guard let name = name(metadata: metadata) else { return false }
37+
guard name.starts(with: "Swift._ContiguousArrayStorage") else {
38+
return false
39+
}
40+
let tr = swift_reflection_typeRefForMetadata(self, UInt(metadata));
41+
guard tr != 0 else {
42+
fatalError("swift_reflection_typeRefForMetadata is null")
43+
return false
44+
}
45+
let genericArgCnt = swift_reflection_genericArgumentCountOfTypeRef(tr);
46+
guard genericArgCnt == 1 else {
47+
fatalError("swift_reflection_genericArgumentCountOfTypeRef is \(genericArgCnt)")
48+
return false
49+
}
50+
51+
let argTr = swift_reflection_genericArgumentOfTypeRef(tr, 0)
52+
guard argTr != 0 else {
53+
fatalError("swift_reflection_genericArgumentOfTypeRef is null")
54+
return false
55+
}
56+
let typeInfo = swift_reflection_infoForTypeRef(self, argTr)
57+
return typeInfo.Kind == SWIFT_STRONG_REFERENCE
58+
}
59+
3460
func name(proto: swift_reflection_ptr_t) -> String? {
3561
guard let cstr = swift_reflection_copyDemangledNameForProtocolDescriptor(
3662
self, proto) else { return nil }

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func dumpGenericMetadata(
5656
let metadatas = allocations.findGenericMetadata(in: context)
5757
let backtraces = backtraceStyle != nil ? context.allocationBacktraces : [:]
5858

59-
print("Address","Allocation","Size","Offset","Name", separator: "\t")
59+
print("Address","Allocation","Size","Offset","isArrayOfClass", "Name", separator: "\t")
6060
for metadata in metadatas {
6161
print("\(hex: metadata.ptr)", terminator: "\t")
6262

@@ -66,6 +66,7 @@ func dumpGenericMetadata(
6666
} else {
6767
print("???\t???\t???", terminator: "\t")
6868
}
69+
print(metadata.isArrayOfClass, terminator: "\t")
6970
print(metadata.name)
7071
if let allocation = metadata.allocation {
7172
printBacktrace(style: backtraceStyle, for: allocation.ptr, in: backtraces, inspector: inspector)

0 commit comments

Comments
 (0)