comparison utils/gdb-scripts/prettyprinters.py @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children c2174574ed3a
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
101 return 'llvm::ArrayRef of length %d' % (self.val['Length']) 101 return 'llvm::ArrayRef of length %d' % (self.val['Length'])
102 102
103 def display_hint (self): 103 def display_hint (self):
104 return 'array' 104 return 'array'
105 105
106 class OptionalPrinter:
107 """Print an llvm::Optional object."""
108
109 def __init__(self, value):
110 self.value = value
111
112 class _iterator:
113 def __init__(self, member, empty):
114 self.member = member
115 self.done = empty
116
117 def __iter__(self):
118 return self
119
120 def next(self):
121 if self.done:
122 raise StopIteration
123 self.done = True
124 return ('value', self.member.dereference())
125
126 def children(self):
127 if not self.value['hasVal']:
128 return self._iterator('', True)
129 return self._iterator(self.value['storage']['buffer'].address.cast(self.value.type.template_argument(0).pointer()), False)
130
131 def to_string(self):
132 return 'llvm::Optional is %sinitialized' % ('' if self.value['hasVal'] else 'not ')
133
134 class DenseMapPrinter:
135 "Print a DenseMap"
136
137 class _iterator:
138 def __init__(self, key_info_t, begin, end):
139 self.key_info_t = key_info_t
140 self.cur = begin
141 self.end = end
142 self.advancePastEmptyBuckets()
143 self.first = True
144
145 def __iter__(self):
146 return self
147
148 def advancePastEmptyBuckets(self):
149 # disabled until the comments below can be addressed
150 # keeping as notes/posterity/hints for future contributors
151 return
152 n = self.key_info_t.name
153 is_equal = gdb.parse_and_eval(n + '::isEqual')
154 empty = gdb.parse_and_eval(n + '::getEmptyKey()')
155 tombstone = gdb.parse_and_eval(n + '::getTombstoneKey()')
156 # the following is invalid, GDB fails with:
157 # Python Exception <class 'gdb.error'> Attempt to take address of value
158 # not located in memory.
159 # because isEqual took parameter (for the unsigned long key I was testing)
160 # by const ref, and GDB
161 # It's also not entirely general - we should be accessing the "getFirst()"
162 # member function, not the 'first' member variable, but I've yet to figure
163 # out how to find/call member functions (especially (const) overloaded
164 # ones) on a gdb.Value.
165 while self.cur != self.end and (is_equal(self.cur.dereference()['first'], empty) or is_equal(self.cur.dereference()['first'], tombstone)):
166 self.cur = self.cur + 1
167
168 def next(self):
169 if self.cur == self.end:
170 raise StopIteration
171 cur = self.cur
172 v = cur.dereference()['first' if self.first else 'second']
173 if not self.first:
174 self.cur = self.cur + 1
175 self.advancePastEmptyBuckets()
176 self.first = True
177 else:
178 self.first = False
179 return 'x', v
180
181 def __init__(self, val):
182 self.val = val
183
184 def children(self):
185 t = self.val.type.template_argument(3).pointer()
186 begin = self.val['Buckets'].cast(t)
187 end = (begin + self.val['NumBuckets']).cast(t)
188 return self._iterator(self.val.type.template_argument(2), begin, end)
189
190 def to_string(self):
191 return 'llvm::DenseMap with %d elements' % (self.val['NumEntries'])
192
193 def display_hint(self):
194 return 'map'
195
196 class TwinePrinter:
197 "Print a Twine"
198
199 def __init__(self, val):
200 self._val = val
201
202 def display_hint(self):
203 return 'string'
204
205 def string_from_pretty_printer_lookup(self, val):
206 '''Lookup the default pretty-printer for val and use it.
207
208 If no pretty-printer is defined for the type of val, print an error and
209 return a placeholder string.'''
210
211 pp = gdb.default_visualizer(val)
212 if pp:
213 s = pp.to_string()
214
215 # The pretty-printer may return a LazyString instead of an actual Python
216 # string. Convert it to a Python string. However, GDB doesn't seem to
217 # register the LazyString type, so we can't check
218 # "type(s) == gdb.LazyString".
219 if 'LazyString' in type(s).__name__:
220 s = s.value().address.string()
221
222 else:
223 print(('No pretty printer for {} found. The resulting Twine ' +
224 'representation will be incomplete.').format(val.type.name))
225 s = '(missing {})'.format(val.type.name)
226
227 return s
228
229 def is_twine_kind(self, kind, expected):
230 if not kind.endswith(expected):
231 return False
232 # apparently some GDB versions add the NodeKind:: namespace
233 # (happens for me on GDB 7.11)
234 return kind in ('llvm::Twine::' + expected,
235 'llvm::Twine::NodeKind::' + expected)
236
237 def string_from_child(self, child, kind):
238 '''Return the string representation of the Twine::Child child.'''
239
240 if self.is_twine_kind(kind, 'EmptyKind') or self.is_twine_kind(kind, 'NullKind'):
241 return ''
242
243 if self.is_twine_kind(kind, 'TwineKind'):
244 return self.string_from_twine_object(child['twine'].dereference())
245
246 if self.is_twine_kind(kind, 'CStringKind'):
247 return child['cString'].string()
248
249 if self.is_twine_kind(kind, 'StdStringKind'):
250 val = child['stdString'].dereference()
251 return self.string_from_pretty_printer_lookup(val)
252
253 if self.is_twine_kind(kind, 'StringRefKind'):
254 val = child['stringRef'].dereference()
255 pp = StringRefPrinter(val)
256 return pp.to_string()
257
258 if self.is_twine_kind(kind, 'SmallStringKind'):
259 val = child['smallString'].dereference()
260 pp = SmallStringPrinter(val)
261 return pp.to_string()
262
263 if self.is_twine_kind(kind, 'CharKind'):
264 return chr(child['character'])
265
266 if self.is_twine_kind(kind, 'DecUIKind'):
267 return str(child['decUI'])
268
269 if self.is_twine_kind(kind, 'DecIKind'):
270 return str(child['decI'])
271
272 if self.is_twine_kind(kind, 'DecULKind'):
273 return str(child['decUL'].dereference())
274
275 if self.is_twine_kind(kind, 'DecLKind'):
276 return str(child['decL'].dereference())
277
278 if self.is_twine_kind(kind, 'DecULLKind'):
279 return str(child['decULL'].dereference())
280
281 if self.is_twine_kind(kind, 'DecLLKind'):
282 return str(child['decLL'].dereference())
283
284 if self.is_twine_kind(kind, 'UHexKind'):
285 val = child['uHex'].dereference()
286 return hex(int(val))
287
288 print(('Unhandled NodeKind {} in Twine pretty-printer. The result will be '
289 'incomplete.').format(kind))
290
291 return '(unhandled {})'.format(kind)
292
293 def string_from_twine_object(self, twine):
294 '''Return the string representation of the Twine object twine.'''
295
296 lhs_str = ''
297 rhs_str = ''
298
299 lhs = twine['LHS']
300 rhs = twine['RHS']
301 lhs_kind = str(twine['LHSKind'])
302 rhs_kind = str(twine['RHSKind'])
303
304 lhs_str = self.string_from_child(lhs, lhs_kind)
305 rhs_str = self.string_from_child(rhs, rhs_kind)
306
307 return lhs_str + rhs_str
308
309 def to_string(self):
310 return self.string_from_twine_object(self._val)
311
106 pp = gdb.printing.RegexpCollectionPrettyPrinter("LLVMSupport") 312 pp = gdb.printing.RegexpCollectionPrettyPrinter("LLVMSupport")
107 pp.add_printer('llvm::SmallString', '^llvm::SmallString<.*>$', SmallStringPrinter) 313 pp.add_printer('llvm::SmallString', '^llvm::SmallString<.*>$', SmallStringPrinter)
108 pp.add_printer('llvm::StringRef', '^llvm::StringRef$', StringRefPrinter) 314 pp.add_printer('llvm::StringRef', '^llvm::StringRef$', StringRefPrinter)
109 pp.add_printer('llvm::SmallVectorImpl', '^llvm::SmallVector(Impl)?<.*>$', SmallVectorPrinter) 315 pp.add_printer('llvm::SmallVectorImpl', '^llvm::SmallVector(Impl)?<.*>$', SmallVectorPrinter)
110 pp.add_printer('llvm::ArrayRef', '^llvm::(Const)?ArrayRef<.*>$', ArrayRefPrinter) 316 pp.add_printer('llvm::ArrayRef', '^llvm::(Const)?ArrayRef<.*>$', ArrayRefPrinter)
317 pp.add_printer('llvm::Optional', '^llvm::Optional<.*>$', OptionalPrinter)
318 pp.add_printer('llvm::DenseMap', '^llvm::DenseMap<.*>$', DenseMapPrinter)
319 pp.add_printer('llvm::Twine', '^llvm::Twine$', TwinePrinter)
111 gdb.printing.register_pretty_printer(gdb.current_objfile(), pp) 320 gdb.printing.register_pretty_printer(gdb.current_objfile(), pp)