blob: 918411db42fbb703e023649dfd2cd2f11584ce04 [file] [log] [blame]
David Blaikie09ec5752016-06-01 01:02:32 +00001import gdb.printing
Fangrui Songa157b8b2018-06-25 23:38:48 +00002
3class Iterator:
4 def __iter__(self):
5 return self
6
7 # Python 2 compatibility
8 def next(self):
9 return self.__next__()
10
11 def children(self):
12 return self
13
Fangrui Song6a906372018-06-26 00:41:49 +000014def escape_bytes(val, l):
15 return '"' + val.string(encoding='Latin-1', length=l).encode('unicode_escape').decode() + '"'
Fangrui Songa157b8b2018-06-25 23:38:48 +000016
David Blaikie09ec5752016-06-01 01:02:32 +000017class SmallStringPrinter:
18 """Print an llvm::SmallString object."""
19
20 def __init__(self, val):
21 self.val = val
22
23 def to_string(self):
24 begin = self.val['BeginX']
Fangrui Songd9c25472018-07-23 21:33:51 +000025 return escape_bytes(begin.cast(gdb.lookup_type('char').pointer()), self.val['Size'])
David Blaikie09ec5752016-06-01 01:02:32 +000026
27class StringRefPrinter:
28 """Print an llvm::StringRef object."""
29
30 def __init__(self, val):
31 self.val = val
32
33 def to_string(self):
Fangrui Song6a906372018-06-26 00:41:49 +000034 return escape_bytes(self.val['Data'], self.val['Length'])
David Blaikie09ec5752016-06-01 01:02:32 +000035
Fangrui Songd9c25472018-07-23 21:33:51 +000036class SmallVectorPrinter(Iterator):
David Blaikie09ec5752016-06-01 01:02:32 +000037 """Print an llvm::SmallVector object."""
38
David Blaikie09ec5752016-06-01 01:02:32 +000039 def __init__(self, val):
40 self.val = val
Fangrui Songd9c25472018-07-23 21:33:51 +000041 t = val.type.template_argument(0).pointer()
42 self.begin = val['BeginX'].cast(t)
43 self.size = val['Size']
44 self.i = 0
David Blaikie09ec5752016-06-01 01:02:32 +000045
Fangrui Songd9c25472018-07-23 21:33:51 +000046 def __next__(self):
47 if self.i == self.size:
48 raise StopIteration
49 ret = '[{}]'.format(self.i), (self.begin+self.i).dereference()
50 self.i += 1
51 return ret
David Blaikie09ec5752016-06-01 01:02:32 +000052
53 def to_string(self):
Fangrui Songd9c25472018-07-23 21:33:51 +000054 return 'llvm::SmallVector of Size {}, Capacity {}'.format(self.size, self.val['Capacity'])
David Blaikie09ec5752016-06-01 01:02:32 +000055
56 def display_hint (self):
57 return 'array'
58
59class ArrayRefPrinter:
60 """Print an llvm::ArrayRef object."""
61
62 class _iterator:
63 def __init__(self, begin, end):
64 self.cur = begin
65 self.end = end
66 self.count = 0
67
68 def __iter__(self):
69 return self
70
71 def next(self):
72 if self.cur == self.end:
73 raise StopIteration
74 count = self.count
75 self.count = self.count + 1
76 cur = self.cur
77 self.cur = self.cur + 1
78 return '[%d]' % count, cur.dereference()
79
Igor Kudrina097a362016-08-05 16:48:31 +000080 __next__ = next
81
David Blaikie09ec5752016-06-01 01:02:32 +000082 def __init__(self, val):
83 self.val = val
84
Philip Pfaffe0566f232018-07-09 18:51:50 +000085 __next__ = next
86
David Blaikie09ec5752016-06-01 01:02:32 +000087 def children(self):
88 data = self.val['Data']
89 return self._iterator(data, data + self.val['Length'])
90
91 def to_string(self):
92 return 'llvm::ArrayRef of length %d' % (self.val['Length'])
93
94 def display_hint (self):
95 return 'array'
96
Fangrui Songa157b8b2018-06-25 23:38:48 +000097class ExpectedPrinter(Iterator):
98 """Print an llvm::Expected object."""
David Blaikiec66e7e32016-12-16 19:16:22 +000099
Fangrui Songa157b8b2018-06-25 23:38:48 +0000100 def __init__(self, val):
101 self.val = val
David Blaikiec66e7e32016-12-16 19:16:22 +0000102
Fangrui Songa157b8b2018-06-25 23:38:48 +0000103 def __next__(self):
104 val = self.val
105 if val is None:
106 raise StopIteration
107 self.val = None
108 if val['HasError']:
109 return ('error', val['ErrorStorage'].address.cast(
110 gdb.lookup_type('llvm::ErrorInfoBase').pointer()).dereference())
111 return ('value', val['TStorage'].address.cast(
112 val.type.template_argument(0).pointer()).dereference())
David Blaikiec66e7e32016-12-16 19:16:22 +0000113
114 def to_string(self):
Fangrui Songa157b8b2018-06-25 23:38:48 +0000115 return 'llvm::Expected{}'.format(' is error' if self.val['HasError'] else '')
116
117class OptionalPrinter(Iterator):
118 """Print an llvm::Optional object."""
119
120 def __init__(self, val):
121 self.val = val
122
123 def __next__(self):
124 val = self.val
125 if val is None:
126 raise StopIteration
127 self.val = None
128 if not val['Storage']['hasVal']:
129 raise StopIteration
130 return ('value', val['Storage']['storage']['buffer'].address.cast(
131 val.type.template_argument(0).pointer()).dereference())
132
133 def to_string(self):
134 return 'llvm::Optional{}'.format('' if self.val['Storage']['hasVal'] else ' is not initialized')
David Blaikiec66e7e32016-12-16 19:16:22 +0000135
David Blaikie23cbb112016-12-16 23:53:14 +0000136class DenseMapPrinter:
137 "Print a DenseMap"
138
139 class _iterator:
140 def __init__(self, key_info_t, begin, end):
141 self.key_info_t = key_info_t
142 self.cur = begin
143 self.end = end
144 self.advancePastEmptyBuckets()
145 self.first = True
146
147 def __iter__(self):
148 return self
149
150 def advancePastEmptyBuckets(self):
151 # disabled until the comments below can be addressed
152 # keeping as notes/posterity/hints for future contributors
153 return
154 n = self.key_info_t.name
155 is_equal = gdb.parse_and_eval(n + '::isEqual')
156 empty = gdb.parse_and_eval(n + '::getEmptyKey()')
157 tombstone = gdb.parse_and_eval(n + '::getTombstoneKey()')
158 # the following is invalid, GDB fails with:
159 # Python Exception <class 'gdb.error'> Attempt to take address of value
160 # not located in memory.
161 # because isEqual took parameter (for the unsigned long key I was testing)
162 # by const ref, and GDB
163 # It's also not entirely general - we should be accessing the "getFirst()"
164 # member function, not the 'first' member variable, but I've yet to figure
165 # out how to find/call member functions (especially (const) overloaded
166 # ones) on a gdb.Value.
167 while self.cur != self.end and (is_equal(self.cur.dereference()['first'], empty) or is_equal(self.cur.dereference()['first'], tombstone)):
168 self.cur = self.cur + 1
169
170 def next(self):
171 if self.cur == self.end:
172 raise StopIteration
173 cur = self.cur
174 v = cur.dereference()['first' if self.first else 'second']
175 if not self.first:
176 self.cur = self.cur + 1
177 self.advancePastEmptyBuckets()
178 self.first = True
179 else:
180 self.first = False
181 return 'x', v
182
Philip Pfaffe0566f232018-07-09 18:51:50 +0000183 __next__ = next
184
David Blaikie23cbb112016-12-16 23:53:14 +0000185 def __init__(self, val):
186 self.val = val
187
188 def children(self):
189 t = self.val.type.template_argument(3).pointer()
190 begin = self.val['Buckets'].cast(t)
191 end = (begin + self.val['NumBuckets']).cast(t)
192 return self._iterator(self.val.type.template_argument(2), begin, end)
193
194 def to_string(self):
195 return 'llvm::DenseMap with %d elements' % (self.val['NumEntries'])
196
197 def display_hint(self):
198 return 'map'
199
David Blaikie718e8ad2017-03-15 20:51:44 +0000200class TwinePrinter:
201 "Print a Twine"
202
203 def __init__(self, val):
204 self._val = val
205
206 def display_hint(self):
207 return 'string'
208
209 def string_from_pretty_printer_lookup(self, val):
210 '''Lookup the default pretty-printer for val and use it.
211
212 If no pretty-printer is defined for the type of val, print an error and
213 return a placeholder string.'''
214
215 pp = gdb.default_visualizer(val)
216 if pp:
217 s = pp.to_string()
218
219 # The pretty-printer may return a LazyString instead of an actual Python
220 # string. Convert it to a Python string. However, GDB doesn't seem to
221 # register the LazyString type, so we can't check
222 # "type(s) == gdb.LazyString".
223 if 'LazyString' in type(s).__name__:
224 s = s.value().address.string()
225
226 else:
227 print(('No pretty printer for {} found. The resulting Twine ' +
228 'representation will be incomplete.').format(val.type.name))
229 s = '(missing {})'.format(val.type.name)
230
231 return s
232
David Blaikie719c94a2017-06-04 03:27:12 +0000233 def is_twine_kind(self, kind, expected):
234 if not kind.endswith(expected):
235 return False
236 # apparently some GDB versions add the NodeKind:: namespace
237 # (happens for me on GDB 7.11)
238 return kind in ('llvm::Twine::' + expected,
239 'llvm::Twine::NodeKind::' + expected)
240
David Blaikie718e8ad2017-03-15 20:51:44 +0000241 def string_from_child(self, child, kind):
242 '''Return the string representation of the Twine::Child child.'''
243
David Blaikie719c94a2017-06-04 03:27:12 +0000244 if self.is_twine_kind(kind, 'EmptyKind') or self.is_twine_kind(kind, 'NullKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000245 return ''
246
David Blaikie719c94a2017-06-04 03:27:12 +0000247 if self.is_twine_kind(kind, 'TwineKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000248 return self.string_from_twine_object(child['twine'].dereference())
249
David Blaikie719c94a2017-06-04 03:27:12 +0000250 if self.is_twine_kind(kind, 'CStringKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000251 return child['cString'].string()
252
David Blaikie719c94a2017-06-04 03:27:12 +0000253 if self.is_twine_kind(kind, 'StdStringKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000254 val = child['stdString'].dereference()
255 return self.string_from_pretty_printer_lookup(val)
256
David Blaikie719c94a2017-06-04 03:27:12 +0000257 if self.is_twine_kind(kind, 'StringRefKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000258 val = child['stringRef'].dereference()
259 pp = StringRefPrinter(val)
260 return pp.to_string()
261
David Blaikie719c94a2017-06-04 03:27:12 +0000262 if self.is_twine_kind(kind, 'SmallStringKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000263 val = child['smallString'].dereference()
264 pp = SmallStringPrinter(val)
265 return pp.to_string()
266
David Blaikie719c94a2017-06-04 03:27:12 +0000267 if self.is_twine_kind(kind, 'CharKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000268 return chr(child['character'])
269
David Blaikie719c94a2017-06-04 03:27:12 +0000270 if self.is_twine_kind(kind, 'DecUIKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000271 return str(child['decUI'])
272
David Blaikie719c94a2017-06-04 03:27:12 +0000273 if self.is_twine_kind(kind, 'DecIKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000274 return str(child['decI'])
275
David Blaikie719c94a2017-06-04 03:27:12 +0000276 if self.is_twine_kind(kind, 'DecULKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000277 return str(child['decUL'].dereference())
278
David Blaikie719c94a2017-06-04 03:27:12 +0000279 if self.is_twine_kind(kind, 'DecLKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000280 return str(child['decL'].dereference())
281
David Blaikie719c94a2017-06-04 03:27:12 +0000282 if self.is_twine_kind(kind, 'DecULLKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000283 return str(child['decULL'].dereference())
284
David Blaikie719c94a2017-06-04 03:27:12 +0000285 if self.is_twine_kind(kind, 'DecLLKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000286 return str(child['decLL'].dereference())
287
David Blaikie719c94a2017-06-04 03:27:12 +0000288 if self.is_twine_kind(kind, 'UHexKind'):
David Blaikie718e8ad2017-03-15 20:51:44 +0000289 val = child['uHex'].dereference()
290 return hex(int(val))
291
292 print(('Unhandled NodeKind {} in Twine pretty-printer. The result will be '
293 'incomplete.').format(kind))
294
295 return '(unhandled {})'.format(kind)
296
297 def string_from_twine_object(self, twine):
298 '''Return the string representation of the Twine object twine.'''
299
300 lhs_str = ''
301 rhs_str = ''
302
303 lhs = twine['LHS']
304 rhs = twine['RHS']
305 lhs_kind = str(twine['LHSKind'])
306 rhs_kind = str(twine['RHSKind'])
307
308 lhs_str = self.string_from_child(lhs, lhs_kind)
309 rhs_str = self.string_from_child(rhs, rhs_kind)
310
311 return lhs_str + rhs_str
312
313 def to_string(self):
314 return self.string_from_twine_object(self._val)
David Blaikie23cbb112016-12-16 23:53:14 +0000315
David Blaikie09ec5752016-06-01 01:02:32 +0000316pp = gdb.printing.RegexpCollectionPrettyPrinter("LLVMSupport")
317pp.add_printer('llvm::SmallString', '^llvm::SmallString<.*>$', SmallStringPrinter)
318pp.add_printer('llvm::StringRef', '^llvm::StringRef$', StringRefPrinter)
319pp.add_printer('llvm::SmallVectorImpl', '^llvm::SmallVector(Impl)?<.*>$', SmallVectorPrinter)
320pp.add_printer('llvm::ArrayRef', '^llvm::(Const)?ArrayRef<.*>$', ArrayRefPrinter)
Fangrui Songa157b8b2018-06-25 23:38:48 +0000321pp.add_printer('llvm::Expected', '^llvm::Expected<.*>$', ExpectedPrinter)
David Blaikiec66e7e32016-12-16 19:16:22 +0000322pp.add_printer('llvm::Optional', '^llvm::Optional<.*>$', OptionalPrinter)
David Blaikie23cbb112016-12-16 23:53:14 +0000323pp.add_printer('llvm::DenseMap', '^llvm::DenseMap<.*>$', DenseMapPrinter)
David Blaikie718e8ad2017-03-15 20:51:44 +0000324pp.add_printer('llvm::Twine', '^llvm::Twine$', TwinePrinter)
David Blaikie09ec5752016-06-01 01:02:32 +0000325gdb.printing.register_pretty_printer(gdb.current_objfile(), pp)