luau/tools/lldb_formatters.py

390 lines
13 KiB
Python
Raw Normal View History

2022-03-12 00:31:18 +08:00
# This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2023-11-11 02:05:48 +08:00
import lldb
2022-03-12 00:31:18 +08:00
# HACK: LLDB's python API doesn't afford anything helpful for getting at variadic template parameters.
# We're forced to resort to parsing names as strings.
2023-11-11 02:05:48 +08:00
2022-03-12 00:31:18 +08:00
def templateParams(s):
depth = 0
2022-09-30 06:11:54 +08:00
start = s.find("<") + 1
2022-03-12 00:31:18 +08:00
result = []
for i, c in enumerate(s[start:], start):
2022-09-30 06:11:54 +08:00
if c == "<":
2022-03-12 00:31:18 +08:00
depth += 1
2022-09-30 06:11:54 +08:00
elif c == ">":
2022-03-12 00:31:18 +08:00
if depth == 0:
2022-09-30 06:11:54 +08:00
result.append(s[start:i].strip())
2022-03-12 00:31:18 +08:00
break
depth -= 1
2022-09-30 06:11:54 +08:00
elif c == "," and depth == 0:
result.append(s[start:i].strip())
2022-03-12 00:31:18 +08:00
start = i + 1
return result
2022-09-30 06:11:54 +08:00
2022-03-12 00:31:18 +08:00
def getType(target, typeName):
stars = 0
typeName = typeName.strip()
2022-09-30 06:11:54 +08:00
while typeName.endswith("*"):
2022-03-12 00:31:18 +08:00
stars += 1
typeName = typeName[:-1]
2022-09-30 06:11:54 +08:00
if typeName.startswith("const "):
2022-03-12 00:31:18 +08:00
typeName = typeName[6:]
ty = target.FindFirstType(typeName.strip())
for _ in range(stars):
ty = ty.GetPointerType()
return ty
2022-09-30 06:11:54 +08:00
2022-03-12 00:31:18 +08:00
def luau_variant_summary(valobj, internal_dict, options):
2022-09-30 06:11:54 +08:00
return valobj.GetChildMemberWithName("type").GetSummary()[1:-1]
2022-03-12 00:31:18 +08:00
class LuauVariantSyntheticChildrenProvider:
node_names = ["type", "value"]
def __init__(self, valobj, internal_dict):
self.valobj = valobj
self.type_index = None
self.current_type = None
self.type_params = []
self.stored_value = None
def num_children(self):
return len(self.node_names)
def has_children(self):
return True
def get_child_index(self, name):
try:
return self.node_names.index(name)
except ValueError:
return -1
def get_child_at_index(self, index):
try:
node = self.node_names[index]
except IndexError:
return None
if node == "type":
if self.current_type:
2022-09-30 06:11:54 +08:00
return self.valobj.CreateValueFromExpression(
node, f'(const char*)"{self.current_type.GetDisplayTypeName()}"'
)
2022-03-12 00:31:18 +08:00
else:
2022-09-30 06:11:54 +08:00
return self.valobj.CreateValueFromExpression(
node, '(const char*)"<unknown type>"'
)
2022-03-12 00:31:18 +08:00
elif node == "value":
if self.stored_value is not None:
if self.current_type is not None:
2022-09-30 06:11:54 +08:00
return self.valobj.CreateValueFromData(
node, self.stored_value.GetData(), self.current_type
)
2022-03-12 00:31:18 +08:00
else:
2022-09-30 06:11:54 +08:00
return self.valobj.CreateValueExpression(
node, '(const char*)"<unknown type>"'
)
2022-03-12 00:31:18 +08:00
else:
2022-09-30 06:11:54 +08:00
return self.valobj.CreateValueFromExpression(
node, '(const char*)"<no stored value>"'
)
2022-03-12 00:31:18 +08:00
else:
return None
def update(self):
2022-09-30 06:11:54 +08:00
self.type_index = self.valobj.GetChildMemberWithName(
"typeId"
).GetValueAsSigned()
self.type_params = templateParams(
self.valobj.GetType().GetCanonicalType().GetName()
)
2022-03-12 00:31:18 +08:00
if len(self.type_params) > self.type_index:
2022-09-30 06:11:54 +08:00
self.current_type = getType(
self.valobj.GetTarget(), self.type_params[self.type_index]
)
2022-03-12 00:31:18 +08:00
if self.current_type:
storage = self.valobj.GetChildMemberWithName("storage")
2022-05-06 07:52:48 +08:00
self.stored_value = storage.Cast(self.current_type)
2022-03-12 00:31:18 +08:00
else:
self.stored_value = None
else:
self.current_type = None
self.stored_value = None
return False
2022-09-30 06:11:54 +08:00
class DenseHashTableSyntheticChildrenProvider:
def __init__(self, valobj, internal_dict):
"""this call should initialize the Python object using valobj as the variable to provide synthetic children for"""
self.valobj = valobj
self.update()
def num_children(self):
"""this call should return the number of children that you want your object to have"""
return self.capacity
def get_child_index(self, name):
"""this call should return the index of the synthetic child whose name is given as argument"""
try:
if name.startswith("[") and name.endswith("]"):
return int(name[1:-1])
else:
return -1
except Exception as e:
print("get_child_index exception", e)
return -1
def get_child_at_index(self, index):
"""this call should return a new LLDB SBValue object representing the child at the index given as argument"""
try:
dataMember = self.valobj.GetChildMemberWithName("data")
data = dataMember.GetPointeeData(index)
return self.valobj.CreateValueFromData(
f"[{index}]",
data,
dataMember.Dereference().GetType(),
)
except Exception as e:
print("get_child_at_index error", e)
def update(self):
"""this call should be used to update the internal state of this Python object whenever the state of the variables in LLDB changes.[1]
Also, this method is invoked before any other method in the interface."""
self.capacity = self.valobj.GetChildMemberWithName(
"capacity"
).GetValueAsUnsigned()
def has_children(self):
"""this call should return True if this object might have children, and False if this object can be guaranteed not to have children.[2]"""
return True
2023-10-21 04:36:26 +08:00
class DenseHashMapSyntheticChildrenProvider:
fixed_names = ["count", "capacity"]
2023-11-11 02:05:48 +08:00
max_expand_children = 100
max_expand_capacity = 1000
2023-10-21 04:36:26 +08:00
def __init__(self, valobj, internal_dict):
self.valobj = valobj
self.count = 0
2023-11-11 02:05:48 +08:00
self.capacity = 0
2023-10-21 04:36:26 +08:00
def num_children(self):
2023-11-11 02:05:48 +08:00
return min(self.max_expand_children, self.count) + len(self.fixed_names)
2023-10-21 04:36:26 +08:00
def get_child_index(self, name):
try:
if name in self.fixed_names:
return self.fixed_names.index(name)
return -1
except Exception as e:
print("get_child_index exception", e, name)
return -1
def get_child_at_index(self, index):
try:
if index < len(self.fixed_names):
fixed_name = self.fixed_names[index]
impl_child = self.valobj.GetValueForExpressionPath(
f".impl.{fixed_name}")
return self.valobj.CreateValueFromData(fixed_name, impl_child.GetData(), impl_child.GetType())
else:
index -= len(self.fixed_names)
2023-11-11 02:05:48 +08:00
empty_key_valobj = self.valobj.GetValueForExpressionPath(
f".impl.empty_key")
key_type = empty_key_valobj.GetType().GetCanonicalType().GetName()
skipped = 0
2023-10-21 04:36:26 +08:00
2023-11-11 02:05:48 +08:00
for slot in range(0, min(self.max_expand_capacity, self.capacity)):
slot_pair = self.valobj.GetValueForExpressionPath(
f".impl.data[{slot}]")
slot_key_valobj = slot_pair.GetChildMemberWithName("first")
2023-10-21 04:36:26 +08:00
2023-11-11 02:05:48 +08:00
eq_test_valobj = self.valobj.EvaluateExpression(
f"*(reinterpret_cast<const {key_type}*>({empty_key_valobj.AddressOf().GetValueAsUnsigned()})) == *(reinterpret_cast<const {key_type}*>({slot_key_valobj.AddressOf().GetValueAsUnsigned()}))")
if eq_test_valobj.GetValue() == "true":
continue
2023-10-21 04:36:26 +08:00
2023-11-11 02:05:48 +08:00
# Skip over previous occupied slots.
if index > skipped:
skipped += 1
2023-10-21 04:36:26 +08:00
continue
2024-04-26 04:57:23 +08:00
return self.valobj.CreateValueFromData(f"[{index}]", slot_pair.GetData(), slot_pair.GetType())
2023-10-21 04:36:26 +08:00
2023-11-11 02:05:48 +08:00
except Exception as e:
print("get_child_at_index error", e, index)
def update(self):
try:
self.capacity = self.count = self.valobj.GetValueForExpressionPath(
".impl.capacity").GetValueAsUnsigned()
self.count = self.valobj.GetValueForExpressionPath(
".impl.count").GetValueAsUnsigned()
2023-10-21 04:36:26 +08:00
except Exception as e:
print("update error", e)
def has_children(self):
return True
class DenseHashSetSyntheticChildrenProvider:
fixed_names = ["count", "capacity"]
2023-11-11 02:05:48 +08:00
max_expand_children = 100
max_expand_capacity = 1000
2023-10-21 04:36:26 +08:00
def __init__(self, valobj, internal_dict):
self.valobj = valobj
self.count = 0
2023-11-11 02:05:48 +08:00
self.capacity = 0
2023-10-21 04:36:26 +08:00
def num_children(self):
2023-11-11 02:05:48 +08:00
return min(self.max_expand_children, self.count) + len(self.fixed_names)
2023-10-21 04:36:26 +08:00
def get_child_index(self, name):
try:
if name in self.fixed_names:
return self.fixed_names.index(name)
return -1
except Exception as e:
print("get_child_index exception", e, name)
return -1
def get_child_at_index(self, index):
try:
if index < len(self.fixed_names):
fixed_name = self.fixed_names[index]
impl_child = self.valobj.GetValueForExpressionPath(
f".impl.{fixed_name}")
return self.valobj.CreateValueFromData(fixed_name, impl_child.GetData(), impl_child.GetType())
else:
index -= len(self.fixed_names)
2023-11-11 02:05:48 +08:00
empty_key_valobj = self.valobj.GetValueForExpressionPath(
f".impl.empty_key")
key_type = empty_key_valobj.GetType().GetCanonicalType().GetName()
skipped = 0
2023-10-21 04:36:26 +08:00
2023-11-11 02:05:48 +08:00
for slot in range(0, min(self.max_expand_capacity, self.capacity)):
slot_valobj = self.valobj.GetValueForExpressionPath(
f".impl.data[{slot}]")
2023-10-21 04:36:26 +08:00
2023-11-11 02:05:48 +08:00
eq_test_valobj = self.valobj.EvaluateExpression(
f"*(reinterpret_cast<const {key_type}*>({empty_key_valobj.AddressOf().GetValueAsUnsigned()})) == *(reinterpret_cast<const {key_type}*>({slot_valobj.AddressOf().GetValueAsUnsigned()}))")
if eq_test_valobj.GetValue() == "true":
continue
2023-10-21 04:36:26 +08:00
2023-11-11 02:05:48 +08:00
# Skip over previous occupied slots.
if index > skipped:
skipped += 1
2023-10-21 04:36:26 +08:00
continue
2023-11-11 02:05:48 +08:00
return self.valobj.CreateValueFromData(f"[{index}]", slot_valobj.GetData(), slot_valobj.GetType())
2023-10-21 04:36:26 +08:00
2023-11-11 02:05:48 +08:00
except Exception as e:
print("get_child_at_index error", e, index)
2023-10-21 04:36:26 +08:00
2023-11-11 02:05:48 +08:00
def update(self):
try:
self.capacity = self.count = self.valobj.GetValueForExpressionPath(
".impl.capacity").GetValueAsUnsigned()
self.count = self.valobj.GetValueForExpressionPath(
".impl.count").GetValueAsUnsigned()
2023-10-21 04:36:26 +08:00
except Exception as e:
print("update error", e)
def has_children(self):
return True
2022-09-30 06:11:54 +08:00
def luau_symbol_summary(valobj, internal_dict, options):
local = valobj.GetChildMemberWithName("local")
2023-10-21 04:36:26 +08:00
global_ = valobj.GetChildMemberWithName(
"global").GetChildMemberWithName("value")
2022-09-30 06:11:54 +08:00
if local.GetValueAsUnsigned() != 0:
return f'local {local.GetChildMemberWithName("name").GetChildMemberWithName("value").GetSummary()}'
elif global_.GetValueAsUnsigned() != 0:
return f"global {global_.GetSummary()}"
else:
return "???"
class AstArraySyntheticChildrenProvider:
def __init__(self, valobj, internal_dict):
self.valobj = valobj
def num_children(self):
return self.size
def get_child_index(self, name):
try:
if name.startswith("[") and name.endswith("]"):
return int(name[1:-1])
else:
return -1
except Exception as e:
print("get_child_index error:", e)
def get_child_at_index(self, index):
try:
dataMember = self.valobj.GetChildMemberWithName("data")
data = dataMember.GetPointeeData(index)
return self.valobj.CreateValueFromData(
f"[{index}]", data, dataMember.Dereference().GetType()
)
except Exception as e:
print("get_child_index error:", e)
def update(self):
2023-10-21 04:36:26 +08:00
self.size = self.valobj.GetChildMemberWithName(
"size").GetValueAsUnsigned()
2022-09-30 06:11:54 +08:00
def has_children(self):
return True
2023-10-21 04:36:26 +08:00
def luau_typepath_property_summary(valobj, internal_dict, options):
name = valobj.GetChildMemberWithName("name").GetSummary()
result = "["
read_write = False
try:
fflag_valobj = valobj.GetFrame().GetValueForVariablePath(
2024-02-24 02:40:00 +08:00
"FFlag::DebugLuauDeferredConstraintResolution::value")
2023-10-21 04:36:26 +08:00
read_write = fflag_valobj.GetValue() == "true"
except Exception as e:
print("luau_typepath_property_summary error:", e)
if read_write:
is_read = valobj.GetChildMemberWithName("isRead").GetValue() == "true"
if is_read:
result += "read "
else:
result += "write "
result += name
result += "]"
return result