]> git.proxmox.com Git - rustc.git/blob - src/etc/gdb_rust_pretty_printing.py
New upstream version 1.20.0+dfsg1
[rustc.git] / src / etc / gdb_rust_pretty_printing.py
1 # Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
4 #
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
10
11 import gdb
12 import re
13 import sys
14 import debugger_pretty_printers_common as rustpp
15
16 # We want a version of `range` which doesn't allocate an intermediate list,
17 # specifically it should use a lazy iterator. In Python 2 this was `xrange`, but
18 # if we're running with Python 3 then we need to use `range` instead.
19 if sys.version_info[0] >= 3:
20 xrange = range
21
22 #===============================================================================
23 # GDB Pretty Printing Module for Rust
24 #===============================================================================
25
26 class GdbType(rustpp.Type):
27
28 def __init__(self, ty):
29 super(GdbType, self).__init__()
30 self.ty = ty
31 self.fields = None
32
33 def get_unqualified_type_name(self):
34 tag = self.ty.tag
35
36 if tag is None:
37 return tag
38
39 return rustpp.extract_type_name(tag).replace("&'static ", "&")
40
41 def get_dwarf_type_kind(self):
42 if self.ty.code == gdb.TYPE_CODE_STRUCT:
43 return rustpp.DWARF_TYPE_CODE_STRUCT
44
45 if self.ty.code == gdb.TYPE_CODE_UNION:
46 return rustpp.DWARF_TYPE_CODE_UNION
47
48 if self.ty.code == gdb.TYPE_CODE_PTR:
49 return rustpp.DWARF_TYPE_CODE_PTR
50
51 if self.ty.code == gdb.TYPE_CODE_ENUM:
52 return rustpp.DWARF_TYPE_CODE_ENUM
53
54 def get_fields(self):
55 assert ((self.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_STRUCT) or
56 (self.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_UNION))
57 if self.fields is None:
58 self.fields = list(self.ty.fields())
59 return self.fields
60
61 def get_wrapped_value(self):
62 return self.ty
63
64
65 class GdbValue(rustpp.Value):
66 def __init__(self, gdb_val):
67 super(GdbValue, self).__init__(GdbType(gdb_val.type))
68 self.gdb_val = gdb_val
69 self.children = {}
70
71 def get_child_at_index(self, index):
72 child = self.children.get(index)
73 if child is None:
74 gdb_field = get_field_at_index(self.gdb_val, index)
75 child = GdbValue(self.gdb_val[gdb_field])
76 self.children[index] = child
77 return child
78
79 def as_integer(self):
80 if self.gdb_val.type.code == gdb.TYPE_CODE_PTR:
81 as_str = rustpp.compat_str(self.gdb_val).split()[0]
82 return int(as_str, 0)
83 return int(self.gdb_val)
84
85 def get_wrapped_value(self):
86 return self.gdb_val
87
88
89 def register_printers(objfile):
90 """Registers Rust pretty printers for the given objfile"""
91 objfile.pretty_printers.append(rust_pretty_printer_lookup_function)
92
93
94 def rust_pretty_printer_lookup_function(gdb_val):
95 """
96 Returns the correct Rust pretty printer for the given value
97 if there is one
98 """
99
100 val = GdbValue(gdb_val)
101 type_kind = val.type.get_type_kind()
102
103 if type_kind == rustpp.TYPE_KIND_EMPTY:
104 return RustEmptyPrinter(val)
105
106 if type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT:
107 return RustStructPrinter(val,
108 omit_first_field = False,
109 omit_type_name = False,
110 is_tuple_like = False)
111
112 if type_kind == rustpp.TYPE_KIND_STRUCT_VARIANT:
113 return RustStructPrinter(val,
114 omit_first_field = True,
115 omit_type_name = False,
116 is_tuple_like = False)
117
118 if type_kind == rustpp.TYPE_KIND_SLICE:
119 return RustSlicePrinter(val)
120
121 if type_kind == rustpp.TYPE_KIND_STR_SLICE:
122 return RustStringSlicePrinter(val)
123
124 if type_kind == rustpp.TYPE_KIND_STD_VEC:
125 return RustStdVecPrinter(val)
126
127 if type_kind == rustpp.TYPE_KIND_STD_STRING:
128 return RustStdStringPrinter(val)
129
130 if type_kind == rustpp.TYPE_KIND_OS_STRING:
131 return RustOsStringPrinter(val)
132
133 if type_kind == rustpp.TYPE_KIND_TUPLE:
134 return RustStructPrinter(val,
135 omit_first_field = False,
136 omit_type_name = True,
137 is_tuple_like = True)
138
139 if type_kind == rustpp.TYPE_KIND_TUPLE_STRUCT:
140 return RustStructPrinter(val,
141 omit_first_field = False,
142 omit_type_name = False,
143 is_tuple_like = True)
144
145 if type_kind == rustpp.TYPE_KIND_CSTYLE_VARIANT:
146 return RustCStyleVariantPrinter(val.get_child_at_index(0))
147
148 if type_kind == rustpp.TYPE_KIND_TUPLE_VARIANT:
149 return RustStructPrinter(val,
150 omit_first_field = True,
151 omit_type_name = False,
152 is_tuple_like = True)
153
154 if type_kind == rustpp.TYPE_KIND_SINGLETON_ENUM:
155 variant = get_field_at_index(gdb_val, 0)
156 return rust_pretty_printer_lookup_function(gdb_val[variant])
157
158 if type_kind == rustpp.TYPE_KIND_REGULAR_ENUM:
159 # This is a regular enum, extract the discriminant
160 discriminant_val = rustpp.get_discriminant_value_as_integer(val)
161 variant = get_field_at_index(gdb_val, discriminant_val)
162 return rust_pretty_printer_lookup_function(gdb_val[variant])
163
164 if type_kind == rustpp.TYPE_KIND_COMPRESSED_ENUM:
165 encoded_enum_info = rustpp.EncodedEnumInfo(val)
166 if encoded_enum_info.is_null_variant():
167 return IdentityPrinter(encoded_enum_info.get_null_variant_name())
168
169 non_null_val = encoded_enum_info.get_non_null_variant_val()
170 return rust_pretty_printer_lookup_function(non_null_val.get_wrapped_value())
171
172 # No pretty printer has been found
173 return None
174
175
176 #=------------------------------------------------------------------------------
177 # Pretty Printer Classes
178 #=------------------------------------------------------------------------------
179 class RustEmptyPrinter(object):
180 def __init__(self, val):
181 self.__val = val
182
183 def to_string(self):
184 return self.__val.type.get_unqualified_type_name()
185
186
187 class RustStructPrinter(object):
188 def __init__(self, val, omit_first_field, omit_type_name, is_tuple_like):
189 self.__val = val
190 self.__omit_first_field = omit_first_field
191 self.__omit_type_name = omit_type_name
192 self.__is_tuple_like = is_tuple_like
193
194 def to_string(self):
195 if self.__omit_type_name:
196 return None
197 return self.__val.type.get_unqualified_type_name()
198
199 def children(self):
200 cs = []
201 wrapped_value = self.__val.get_wrapped_value()
202
203 for number, field in enumerate(self.__val.type.get_fields()):
204 field_value = wrapped_value[field.name]
205 if self.__is_tuple_like:
206 cs.append((str(number), field_value))
207 else:
208 cs.append((field.name, field_value))
209
210 if self.__omit_first_field:
211 cs = cs[1:]
212
213 return cs
214
215 def display_hint(self):
216 if self.__is_tuple_like:
217 return "array"
218 else:
219 return ""
220
221
222 class RustSlicePrinter(object):
223 def __init__(self, val):
224 self.__val = val
225
226 @staticmethod
227 def display_hint():
228 return "array"
229
230 def to_string(self):
231 (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
232 return (self.__val.type.get_unqualified_type_name() +
233 ("(len: %i)" % length))
234
235 def children(self):
236 (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
237 assert data_ptr.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_PTR
238 raw_ptr = data_ptr.get_wrapped_value()
239
240 for index in xrange(0, length):
241 yield (str(index), (raw_ptr + index).dereference())
242
243
244 class RustStringSlicePrinter(object):
245 def __init__(self, val):
246 self.__val = val
247
248 def to_string(self):
249 (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
250 raw_ptr = data_ptr.get_wrapped_value()
251 return '"%s"' % raw_ptr.string(encoding="utf-8", length=length)
252
253
254 class RustStdVecPrinter(object):
255 def __init__(self, val):
256 self.__val = val
257
258 @staticmethod
259 def display_hint():
260 return "array"
261
262 def to_string(self):
263 (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(self.__val)
264 return (self.__val.type.get_unqualified_type_name() +
265 ("(len: %i, cap: %i)" % (length, cap)))
266
267 def children(self):
268 (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(self.__val)
269 gdb_ptr = data_ptr.get_wrapped_value()
270 for index in xrange(0, length):
271 yield (str(index), (gdb_ptr + index).dereference())
272
273
274 class RustStdStringPrinter(object):
275 def __init__(self, val):
276 self.__val = val
277
278 def to_string(self):
279 vec = self.__val.get_child_at_index(0)
280 (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(vec)
281 return '"%s"' % data_ptr.get_wrapped_value().string(encoding="utf-8",
282 length=length)
283
284
285 class RustOsStringPrinter(object):
286 def __init__(self, val):
287 self.__val = val
288
289 def to_string(self):
290 buf = self.__val.get_child_at_index(0)
291 vec = buf.get_child_at_index(0)
292 if vec.type.get_unqualified_type_name() == "Wtf8Buf":
293 vec = vec.get_child_at_index(0)
294
295 (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(
296 vec)
297 return '"%s"' % data_ptr.get_wrapped_value().string(length=length)
298
299
300 class RustCStyleVariantPrinter(object):
301 def __init__(self, val):
302 assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_ENUM
303 self.__val = val
304
305 def to_string(self):
306 return str(self.__val.get_wrapped_value())
307
308
309 class IdentityPrinter(object):
310 def __init__(self, string):
311 self.string = string
312
313 def to_string(self):
314 return self.string
315
316
317 def get_field_at_index(gdb_val, index):
318 i = 0
319 for field in gdb_val.type.fields():
320 if i == index:
321 return field
322 i += 1
323 return None