2 LLDB Formatters for LLVM data types.
4 Load into LLDB with 'command script import /path/to/lldbDataFormatters.py'
9 def __lldb_init_module(debugger
, internal_dict
):
10 debugger
.HandleCommand('type category define -e llvm -l c++')
11 debugger
.HandleCommand('type synthetic add -w llvm '
12 '-l lldbDataFormatters.SmallVectorSynthProvider '
13 '-x "^llvm::SmallVectorImpl<.+>$"')
14 debugger
.HandleCommand('type synthetic add -w llvm '
15 '-l lldbDataFormatters.SmallVectorSynthProvider '
16 '-x "^llvm::SmallVector<.+,.+>$"')
17 debugger
.HandleCommand('type synthetic add -w llvm '
18 '-l lldbDataFormatters.ArrayRefSynthProvider '
19 '-x "^llvm::ArrayRef<.+>$"')
20 debugger
.HandleCommand('type synthetic add -w llvm '
21 '-l lldbDataFormatters.OptionalSynthProvider '
22 '-x "^llvm::Optional<.+>$"')
23 debugger
.HandleCommand('type summary add -w llvm '
24 '-F lldbDataFormatters.OptionalSummaryProvider '
25 '-x "^llvm::Optional<.+>$"')
26 debugger
.HandleCommand('type summary add -w llvm '
27 '-F lldbDataFormatters.SmallStringSummaryProvider '
28 '-x "^llvm::SmallString<.+>$"')
29 debugger
.HandleCommand('type summary add -w llvm '
30 '-F lldbDataFormatters.StringRefSummaryProvider '
31 '-x "^llvm::StringRef$"')
32 debugger
.HandleCommand('type summary add -w llvm '
33 '-F lldbDataFormatters.ConstStringSummaryProvider '
34 '-x "^lldb_private::ConstString$"')
36 # Pretty printer for llvm::SmallVector/llvm::SmallVectorImpl
37 class SmallVectorSynthProvider
:
38 def __init__(self
, valobj
, internal_dict
):
40 self
.update() # initialize this provider
42 def num_children(self
):
43 return self
.size
.GetValueAsUnsigned(0)
45 def get_child_index(self
, name
):
47 return int(name
.lstrip('[').rstrip(']'))
51 def get_child_at_index(self
, index
):
55 if index
>= self
.num_children():
58 offset
= index
* self
.type_size
59 return self
.begin
.CreateChildAtOffset('['+str(index
)+']',
60 offset
, self
.data_type
)
63 self
.begin
= self
.valobj
.GetChildMemberWithName('BeginX')
64 self
.size
= self
.valobj
.GetChildMemberWithName('Size')
65 the_type
= self
.valobj
.GetType()
66 # If this is a reference type we have to dereference it to get to the
68 if the_type
.IsReferenceType():
69 the_type
= the_type
.GetDereferencedType()
71 self
.data_type
= the_type
.GetTemplateArgumentType(0)
72 self
.type_size
= self
.data_type
.GetByteSize()
73 assert self
.type_size
!= 0
75 class ArrayRefSynthProvider
:
76 """ Provider for llvm::ArrayRef """
77 def __init__(self
, valobj
, internal_dict
):
79 self
.update() # initialize this provider
81 def num_children(self
):
84 def get_child_index(self
, name
):
86 return int(name
.lstrip('[').rstrip(']'))
90 def get_child_at_index(self
, index
):
91 if index
< 0 or index
>= self
.num_children():
93 offset
= index
* self
.type_size
94 return self
.data
.CreateChildAtOffset('[' + str(index
) + ']',
95 offset
, self
.data_type
)
98 self
.data
= self
.valobj
.GetChildMemberWithName('Data')
99 length_obj
= self
.valobj
.GetChildMemberWithName('Length')
100 self
.length
= length_obj
.GetValueAsUnsigned(0)
101 self
.data_type
= self
.data
.GetType().GetPointeeType()
102 self
.type_size
= self
.data_type
.GetByteSize()
103 assert self
.type_size
!= 0
105 def GetOptionalValue(valobj
):
106 storage
= valobj
.GetChildMemberWithName('Storage')
111 hasVal
= storage
.GetChildMemberWithName('hasVal').GetValueAsUnsigned(failure
)
112 if hasVal
== failure
:
113 return '<could not read llvm::Optional>'
118 underlying_type
= storage
.GetType().GetTemplateArgumentType(0)
119 storage
= storage
.GetChildMemberWithName('value')
120 return storage
.Cast(underlying_type
)
122 def OptionalSummaryProvider(valobj
, internal_dict
):
123 val
= GetOptionalValue(valobj
)
124 return val
.summary
if val
else 'None'
126 class OptionalSynthProvider
:
127 """Provides deref support to llvm::Optional<T>"""
128 def __init__(self
, valobj
, internal_dict
):
131 def num_children(self
):
132 return self
.valobj
.num_children
134 def get_child_index(self
, name
):
135 if name
== '$$dereference$$':
136 return self
.valobj
.num_children
137 return self
.valobj
.GetIndexOfChildWithName(name
)
139 def get_child_at_index(self
, index
):
140 if index
< self
.valobj
.num_children
:
141 return self
.valobj
.GetChildAtIndex(index
)
142 return GetOptionalValue(self
.valobj
) or lldb
.SBValue()
144 def SmallStringSummaryProvider(valobj
, internal_dict
):
145 num_elements
= valobj
.GetNumChildren()
147 for i
in range(0, num_elements
):
148 c
= valobj
.GetChildAtIndex(i
).GetValue()
154 def StringRefSummaryProvider(valobj
, internal_dict
):
155 if valobj
.GetNumChildren() == 2:
156 # StringRef's are also used to point at binary blobs in memory,
157 # so filter out suspiciously long strings.
159 length
= valobj
.GetChildAtIndex(1).GetValueAsUnsigned(max_length
)
162 if length
< max_length
:
163 return valobj
.GetChildAtIndex(0).GetSummary()
166 def ConstStringSummaryProvider(valobj
, internal_dict
):
167 if valobj
.GetNumChildren() == 1:
168 return valobj
.GetChildAtIndex(0).GetSummary()