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.
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.
14 import debugger_pretty_printers_common as rustpp
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.major >= 3:
22 #===============================================================================
23 # GDB Pretty Printing Module for Rust
24 #===============================================================================
26 class GdbType(rustpp.Type):
28 def __init__(self, ty):
29 super(GdbType, self).__init__()
33 def get_unqualified_type_name(self):
39 return tag.replace("&'static ", "&")
41 def get_dwarf_type_kind(self):
42 if self.ty.code == gdb.TYPE_CODE_STRUCT:
43 return rustpp.DWARF_TYPE_CODE_STRUCT
45 if self.ty.code == gdb.TYPE_CODE_UNION:
46 return rustpp.DWARF_TYPE_CODE_UNION
48 if self.ty.code == gdb.TYPE_CODE_PTR:
49 return rustpp.DWARF_TYPE_CODE_PTR
51 if self.ty.code == gdb.TYPE_CODE_ENUM:
52 return rustpp.DWARF_TYPE_CODE_ENUM
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())
61 def get_wrapped_value(self):
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
71 def get_child_at_index(self, index):
72 child = self.children.get(index)
74 gdb_field = get_field_at_index(self.gdb_val, index)
75 child = GdbValue(self.gdb_val[gdb_field])
76 self.children[index] = child
80 if self.gdb_val.type.code == gdb.TYPE_CODE_PTR:
81 return int(str(self.gdb_val), 0)
82 return int(self.gdb_val)
84 def get_wrapped_value(self):
88 def register_printers(objfile):
89 """Registers Rust pretty printers for the given objfile"""
90 objfile.pretty_printers.append(rust_pretty_printer_lookup_function)
93 def rust_pretty_printer_lookup_function(gdb_val):
95 Returns the correct Rust pretty printer for the given value
99 val = GdbValue(gdb_val)
100 type_kind = val.type.get_type_kind()
102 if (type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT or
103 type_kind == rustpp.TYPE_KIND_EMPTY):
104 return RustStructPrinter(val,
105 omit_first_field = False,
106 omit_type_name = False,
107 is_tuple_like = False)
109 if type_kind == rustpp.TYPE_KIND_STRUCT_VARIANT:
110 return RustStructPrinter(val,
111 omit_first_field = True,
112 omit_type_name = False,
113 is_tuple_like = False)
115 if type_kind == rustpp.TYPE_KIND_SLICE:
116 return RustSlicePrinter(val)
118 if type_kind == rustpp.TYPE_KIND_STR_SLICE:
119 return RustStringSlicePrinter(val)
121 if type_kind == rustpp.TYPE_KIND_STD_VEC:
122 return RustStdVecPrinter(val)
124 if type_kind == rustpp.TYPE_KIND_STD_STRING:
125 return RustStdStringPrinter(val)
127 if type_kind == rustpp.TYPE_KIND_TUPLE:
128 return RustStructPrinter(val,
129 omit_first_field = False,
130 omit_type_name = True,
131 is_tuple_like = True)
133 if type_kind == rustpp.TYPE_KIND_TUPLE_STRUCT:
134 return RustStructPrinter(val,
135 omit_first_field = False,
136 omit_type_name = False,
137 is_tuple_like = True)
139 if type_kind == rustpp.TYPE_KIND_CSTYLE_VARIANT:
140 return RustCStyleVariantPrinter(val.get_child_at_index(0))
142 if type_kind == rustpp.TYPE_KIND_TUPLE_VARIANT:
143 return RustStructPrinter(val,
144 omit_first_field = True,
145 omit_type_name = False,
146 is_tuple_like = True)
148 if type_kind == rustpp.TYPE_KIND_SINGLETON_ENUM:
149 variant = get_field_at_index(gdb_val, 0)
150 return rust_pretty_printer_lookup_function(gdb_val[variant])
152 if type_kind == rustpp.TYPE_KIND_REGULAR_ENUM:
153 # This is a regular enum, extract the discriminant
154 discriminant_val = rustpp.get_discriminant_value_as_integer(val)
155 variant = get_field_at_index(gdb_val, discriminant_val)
156 return rust_pretty_printer_lookup_function(gdb_val[variant])
158 if type_kind == rustpp.TYPE_KIND_COMPRESSED_ENUM:
159 encoded_enum_info = rustpp.EncodedEnumInfo(val)
160 if encoded_enum_info.is_null_variant():
161 return IdentityPrinter(encoded_enum_info.get_null_variant_name())
163 non_null_val = encoded_enum_info.get_non_null_variant_val()
164 return rust_pretty_printer_lookup_function(non_null_val.get_wrapped_value())
166 # No pretty printer has been found
170 #=------------------------------------------------------------------------------
171 # Pretty Printer Classes
172 #=------------------------------------------------------------------------------
173 class RustStructPrinter:
174 def __init__(self, val, omit_first_field, omit_type_name, is_tuple_like):
176 self.__omit_first_field = omit_first_field
177 self.__omit_type_name = omit_type_name
178 self.__is_tuple_like = is_tuple_like
181 if self.__omit_type_name:
183 return self.__val.type.get_unqualified_type_name()
187 wrapped_value = self.__val.get_wrapped_value()
189 for field in self.__val.type.get_fields():
190 field_value = wrapped_value[field.name]
191 if self.__is_tuple_like:
192 cs.append(("", field_value))
194 cs.append((field.name, field_value))
196 if self.__omit_first_field:
201 def display_hint(self):
202 if self.__is_tuple_like:
208 class RustSlicePrinter:
209 def __init__(self, val):
212 def display_hint(self):
216 (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
217 return (self.__val.type.get_unqualified_type_name() +
218 ("(len: %i)" % length))
221 (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
222 assert data_ptr.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_PTR
223 raw_ptr = data_ptr.get_wrapped_value()
225 for index in xrange(0, length):
226 yield (str(index), (raw_ptr + index).dereference())
229 class RustStringSlicePrinter:
230 def __init__(self, val):
234 (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
235 raw_ptr = data_ptr.get_wrapped_value()
236 return '"%s"' % raw_ptr.string(encoding="utf-8", length=length)
239 class RustStdVecPrinter:
240 def __init__(self, val):
243 def display_hint(self):
247 (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(self.__val)
248 return (self.__val.type.get_unqualified_type_name() +
249 ("(len: %i, cap: %i)" % (length, cap)))
252 (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(self.__val)
253 gdb_ptr = data_ptr.get_wrapped_value()
254 for index in xrange(0, length):
255 yield (str(index), (gdb_ptr + index).dereference())
258 class RustStdStringPrinter:
259 def __init__(self, val):
263 vec = self.__val.get_child_at_index(0)
264 (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(vec)
265 return '"%s"' % data_ptr.get_wrapped_value().string(encoding="utf-8",
269 class RustCStyleVariantPrinter:
270 def __init__(self, val):
271 assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_ENUM
275 return str(self.__val.get_wrapped_value())
278 class IdentityPrinter:
279 def __init__(self, string):
286 def get_field_at_index(gdb_val, index):
288 for field in gdb_val.type.fields():