Skip to content

[API] add GetSyntheticValue #95959

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 17 commits into from
Jul 15, 2024
2 changes: 2 additions & 0 deletions lldb/include/lldb/API/SBValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class LLDB_API SBValue {

lldb::SBValue GetNonSyntheticValue();

lldb::SBValue GetSyntheticValue();

lldb::DynamicValueType GetPreferDynamicValue();

void SetPreferDynamicValue(lldb::DynamicValueType use_dynamic);
Expand Down
15 changes: 15 additions & 0 deletions lldb/source/API/SBValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,21 @@ lldb::SBValue SBValue::GetNonSyntheticValue() {
return value_sb;
}

lldb::SBValue SBValue::GetSyntheticValue() {
LLDB_INSTRUMENT_VA(this);

SBValue value_sb;
if (IsValid()) {
ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(),
m_opaque_sp->GetUseDynamic(), true));
value_sb.SetSP(proxy_sp);
if (!value_sb.IsSynthetic()) {
return {};
}
}
return value_sb;
}

lldb::DynamicValueType SBValue::GetPreferDynamicValue() {
LLDB_INSTRUMENT_VA(this);

Expand Down
22 changes: 22 additions & 0 deletions lldb/test/API/python_api/formatters/TestFormattersSBAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ def cleanup():
self.dbg.GetCategory("JASSynth").SetEnabled(True)
self.expect("frame variable foo", matching=True, substrs=["X = 1"])

self.dbg.GetCategory("CCCSynth2").SetEnabled(True)
self.expect(
"frame variable ccc",
matching=True,
substrs=[
"CCC object with leading synthetic value (int) b = 222",
"a = 111",
"b = 222",
"c = 333",
],
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
)
)
self.dbg.GetCategory("CCCSynth").SetEnabled(False)

I'm not sure how lldb chooses the summary providers in case of conflicts, but since that's not what we're testing here, we might as well remove the ambiguity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we can have a conflict. In this test we have several categories which are enabled one after the other. I just add a new one (with a new name CCCSynth2).

Copy link
Collaborator

Choose a reason for hiding this comment

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

You have two categories, but they both have a summary provider for the same type (CCC). (Enabling one category does not automatically disable the other ones.) Right now, lldb seems to pick the one you want, but I don't think we promise that anywhere (in fact, I wouldn't be surprised if this comes down to some (nondeterministic) ordering in some hash container). By disabling the first category, we make sure that lldb always pick the one we want (in case, e.g., the container ordering changes). Alternatively, you could just attach the summary provider to a different type (CCC2?)

self.dbg.GetCategory("CCCSynth2").SetEnabled(False)

self.dbg.GetCategory("CCCSynth").SetEnabled(True)
self.expect(
"frame variable ccc",
Expand All @@ -155,6 +168,15 @@ def cleanup():
],
)

self.dbg.GetCategory("BarIntSynth").SetEnabled(True)
self.expect(
"frame variable bar_int",
matching=True,
substrs=[
"(int) bar_int = 20 bar_int synthetic: No value",
],
)

foo_var = (
self.dbg.GetSelectedTarget()
.GetProcess()
Expand Down
2 changes: 2 additions & 0 deletions lldb/test/API/python_api/formatters/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ int main(int argc, char const *argv[]) {

CCC ccc = {111, 222, 333};

int bar_int = 20;

Empty1 e1;
Empty2 e2;

Expand Down
44 changes: 42 additions & 2 deletions lldb/test/API/python_api/formatters/synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,28 @@ def ccc_summary(sbvalue, internal_dict):
# This tests that the SBValue.GetNonSyntheticValue() actually returns a
# non-synthetic value. If it does not, then sbvalue.GetChildMemberWithName("a")
# in the following statement will call the 'get_child_index' method of the
# synthetic child provider CCCSynthProvider below (which raises an
# exception).
# synthetic child provider CCCSynthProvider below (which return the "b" field").
return "CCC object with leading value " + str(sbvalue.GetChildMemberWithName("a"))


def ccc_synthetic(sbvalue, internal_dict):
sbvalue = sbvalue.GetSyntheticValue()
# This tests that the SBValue.GetSyntheticValue() actually returns a
# synthetic value. If it does, then sbvalue.GetChildMemberWithName("a")
# in the following statement will call the 'get_child_index' method of the
# synthetic child provider CCCSynthProvider below (which return the "b" field").
return "CCC object with leading synthetic value " + str(
sbvalue.GetChildMemberWithName("a")
)


def bar_int_synthetic(sbvalue, internal_dict):
sbvalue = sbvalue.GetSyntheticValue()
# This tests that the SBValue.GetSyntheticValue() actually returns no
# value when the value has no synthetic representation.
return "bar_int synthetic: " + str(sbvalue)


class CCCSynthProvider(object):
def __init__(self, sbvalue, internal_dict):
self._sbvalue = sbvalue
Expand All @@ -42,6 +59,9 @@ def num_children(self):
return 3

def get_child_index(self, name):
if name == "a":
# Return b for test.
return 1
raise RuntimeError("I don't want to be called!")

def get_child_at_index(self, index):
Expand Down Expand Up @@ -119,3 +139,23 @@ def __lldb_init_module(debugger, dict):
"synth.empty2_summary", lldb.eTypeOptionHideEmptyAggregates
),
)
cat2 = debugger.CreateCategory("CCCSynth2")
cat2.AddTypeSynthetic(
lldb.SBTypeNameSpecifier("CCC"),
lldb.SBTypeSynthetic.CreateWithClassName(
"synth.CCCSynthProvider", lldb.eTypeOptionCascade
),
)
cat2.AddTypeSummary(
lldb.SBTypeNameSpecifier("CCC"),
lldb.SBTypeSummary.CreateWithFunctionName(
"synth.ccc_synthetic", lldb.eTypeOptionCascade
),
)
cat3 = debugger.CreateCategory("BarIntSynth")
cat3.AddTypeSummary(
lldb.SBTypeNameSpecifier("int"),
lldb.SBTypeSummary.CreateWithFunctionName(
"synth.bar_int_synthetic", lldb.eTypeOptionCascade
),
)