Skip to content

[lldb] Support frame variable for generic types in embedded Swift #8267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ using namespace lldb_private;
using namespace lldb_private::dwarf;
using namespace lldb_private::plugin::dwarf;

/// Given a die to a substituted generic Swift type, return the analogous
/// unsubstituted version.
///
/// Given a generic type (for example, a generic Pair), the compiler will emit
/// full debug information for the unsubstituted type (Pair<T, U>), and opaque
/// debug information for each every specialization (for example, Pair<Int,
/// Double>), with a link back to the unsubstituted type. When looking up one of
/// the specialized generics, return the unsubstituted version instead.
static std::optional<std::pair<CompilerType, DWARFDIE>>
findUnsubstitutedGenericTypeAndDIE(TypeSystemSwiftTypeRef &ts,
const DWARFDIE &die) {
auto unsubstituted_die =
die.GetAttributeValueAsReferenceDIE(llvm::dwarf::DW_AT_specification);
if (!unsubstituted_die)
return {};

const auto *mangled_name = unsubstituted_die.GetAttributeValueAsString(
llvm::dwarf::DW_AT_linkage_name, nullptr);
assert(mangled_name);
auto unsubstituted_type =
ts.GetTypeFromMangledTypename(ConstString(mangled_name));
return {{unsubstituted_type, unsubstituted_die}};
}
/// Given a type system and a typeref, return the compiler type and die of the
/// type that matches that mangled name, looking up the in the type system's
/// module's debug information.
Expand Down Expand Up @@ -62,6 +85,10 @@ getTypeAndDie(TypeSystemSwiftTypeRef &ts,
return {};
}
auto die = dwarf->GetDIE(lldb_type->GetID());

if (auto unsubstituted_pair = findUnsubstitutedGenericTypeAndDIE(ts, die))
return unsubstituted_pair;

return {{type, die}};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def test(self):

self.expect(
"frame variable varB",
substrs=["varB = ", "a = (field = 4.2000000000000002)", "b = 123456"],
substrs=["varB = ", "a = (field = 4.5)", "b = 123456"],
)
self.expect(
"frame variable tuple",
substrs=[
"(a.A, a.B) tuple = {",
"0 = (field = 4.2000000000000002)",
"0 = (field = 4.5)",
"1 = {",
"a = (field = 4.2000000000000002)",
"a = (field = 4.5)",
"b = 123456",
],
)
Expand All @@ -41,7 +41,7 @@ def test(self):
substrs=[
"SinglePayloadEnum) singlePayload = ",
"payload {",
"a = (field = 4.2000000000000002)",
"a = (field = 4.5)",
"b = 123456",
],
)
Expand Down Expand Up @@ -85,7 +85,7 @@ def test(self):
"frame variable fullMultipayloadEnum2",
substrs=[
"FullMultipayloadEnum) fullMultipayloadEnum2 = ",
"(two = 9.2100000000000008)",
"(two = 9.5)",
],
)

Expand All @@ -100,7 +100,7 @@ def test(self):
"frame variable bigFullMultipayloadEnum2",
substrs=[
"a.BigFullMultipayloadEnum) bigFullMultipayloadEnum2 = two {",
"two = (0 = 452.19999999999999, 1 = 753.89999999999998)",
"two = (0 = 452.5, 1 = 753.5)",
],
)

Expand All @@ -112,7 +112,7 @@ def test(self):
"Sup = {",
"supField = 42",
"subField = {",
"a = (field = 4.2000000000000002",
"a = (field = 4.5",
"b = 123456",
],
)
Expand All @@ -124,8 +124,66 @@ def test(self):
"a.Sup = {",
"supField = 42",
"subField = {",
"a = (field = 4.2000000000000002",
"a = (field = 4.5",
"b = 123456",
"subSubField = (field = 4.2000000000000002)",
"subSubField = (field = 4.5)",
],
)

self.expect(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more stable to use lldbutil.check_variable here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check_variable doesn't allow for checking for substrings, which I need in most of these tests

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant was to write this as a chain of

var = frame.FindVariable("foo")
child = var.GetChildAtIndex(0)
lldbutil.check_var(child, ...)

So each check is precise and not depending on the formatting of the aggregate type.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll do that in an NFC patch, as that file has a lot of checks and will take a while to convert them all

"frame variable gsp",
substrs=[
"GenericStructPair<Int, Double>) gsp =",
"(t = 42, u = 94.5)",
],
)

self.expect(
"frame variable gsp2",
substrs=[
"a.GenericStructPair<a.Sup, a.B>) gsp2 = {",
"t = ",
"(supField = 42)",
"u = {",
"a = (field = 4.5)",
"b = 123456",
],
)

self.expect(
"frame variable gsp3",
substrs=[
"(a.GenericStructPair<a.BigFullMultipayloadEnum,",
"a.SmallMultipayloadEnum>) gsp3 = {",
"t = one {",
"one = (0 = 209, 1 = 315)",
"u = two {",
],
)

self.expect(
"frame variable gcp",
substrs=[
"GenericClassPair<Double, Int>) gcp =",
"(t = 43.799999999999997, u = 9348)",
],
)

self.expect(
"frame variable either",
substrs=["(a.Either<Int, Double>) either =", "left (left = 1234)"],
)

self.expect(
"frame variable either2",
substrs=[
"(a.Either<a.Sup, a.GenericStructPair<a.BigFullMultipayloadEnum,",
"a.SmallMultipayloadEnum>>)",
"either2 = right {",
"right = {",
"t = one {",
"one = (0 = 209, 1 = 315)",
"u = two {",
"two = one",
],
)
33 changes: 30 additions & 3 deletions lldb/test/API/lang/swift/embedded/frame_variable/main.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct A {
let field = 4.2
let field = 4.5
}

struct B {
Expand Down Expand Up @@ -62,6 +62,27 @@ class SubSub: Sub {
var subSubField = A()
}


struct GenericStructPair<T, U> {
let t: T
let u: U
}

class GenericClassPair<T, U> {
let t: T
let u: U

init(t: T, u: U) {
self.t = t
self.u = u
}
}

enum Either<Left, Right> {
case left(Left)
case right(Right)
}

let varB = B()
let tuple = (A(), B())
let trivial = TrivialEnum.theCase
Expand All @@ -78,13 +99,19 @@ let e3 = Sup()
e3.supField = 44
let bigMultipayloadEnum1 = BigMultipayloadEnum.one(e1, e2, e3)
let fullMultipayloadEnum1 = FullMultipayloadEnum.one(120)
let fullMultipayloadEnum2 = FullMultipayloadEnum.two(9.21)
let fullMultipayloadEnum2 = FullMultipayloadEnum.two(9.5)
let bigFullMultipayloadEnum1 = BigFullMultipayloadEnum.one(209, 315)
let bigFullMultipayloadEnum2 = BigFullMultipayloadEnum.two(452.2, 753.9)
let bigFullMultipayloadEnum2 = BigFullMultipayloadEnum.two(452.5, 753.5)
let sup = Sup()
let sub = Sub()
let subSub = SubSub()
let sup2: Sup = SubSub()
let gsp = GenericStructPair(t: 42, u: 94.5)
let gsp2 = GenericStructPair(t: Sup(), u: B())
let gsp3 = GenericStructPair(t: bigFullMultipayloadEnum1, u: smallMultipayloadEnum2)
let gcp = GenericClassPair(t: 43.8, u: 9348)
let either = Either<Int, Double>.left(1234)
let either2 = Either<Sup, _>.right(gsp3)

// Dummy statement to set breakpoint print can't be used in embedded Swift for now.
let dummy = A() // break here
Expand Down