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.
13 import debugger_pretty_printers_common as rustpp
15 #===============================================================================
16 # GDB Pretty Printing Module for Rust
17 #===============================================================================
19 class GdbType(rustpp.Type):
21 def __init__(self, ty):
22 super(GdbType, self).__init__()
26 def get_unqualified_type_name(self):
32 return tag.replace("&'static ", "&")
34 def get_dwarf_type_kind(self):
35 if self.ty.code == gdb.TYPE_CODE_STRUCT:
36 return rustpp.DWARF_TYPE_CODE_STRUCT
38 if self.ty.code == gdb.TYPE_CODE_UNION:
39 return rustpp.DWARF_TYPE_CODE_UNION
41 if self.ty.code == gdb.TYPE_CODE_PTR:
42 return rustpp.DWARF_TYPE_CODE_PTR
44 if self.ty.code == gdb.TYPE_CODE_ENUM:
45 return rustpp.DWARF_TYPE_CODE_ENUM
48 assert ((self.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_STRUCT) or
49 (self.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_UNION))
50 if self.fields is None:
51 self.fields = list(self.ty.fields())
54 def get_wrapped_value(self):
58 class GdbValue(rustpp.Value):
59 def __init__(self, gdb_val):
60 super(GdbValue, self).__init__(GdbType(gdb_val.type))
61 self.gdb_val = gdb_val
64 def get_child_at_index(self, index):
65 child = self.children.get(index)
67 gdb_field = get_field_at_index(self.gdb_val, index)
68 child = GdbValue(self.gdb_val[gdb_field])
69 self.children[index] = child
73 return int(self.gdb_val)
75 def get_wrapped_value(self):
79 def register_printers(objfile):
80 """Registers Rust pretty printers for the given objfile"""
81 objfile.pretty_printers.append(rust_pretty_printer_lookup_function)
84 def rust_pretty_printer_lookup_function(gdb_val):
86 Returns the correct Rust pretty printer for the given value
90 val = GdbValue(gdb_val)
91 type_kind = val.type.get_type_kind()
93 if (type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT or
94 type_kind == rustpp.TYPE_KIND_EMPTY):
95 return RustStructPrinter(val,
96 omit_first_field = False,
97 omit_type_name = False,
98 is_tuple_like = False)
100 if type_kind == rustpp.TYPE_KIND_STRUCT_VARIANT:
101 return RustStructPrinter(val,
102 omit_first_field = True,
103 omit_type_name = False,
104 is_tuple_like = False)
106 if type_kind == rustpp.TYPE_KIND_SLICE:
107 return RustSlicePrinter(val)
109 if type_kind == rustpp.TYPE_KIND_STR_SLICE:
110 return RustStringSlicePrinter(val)
112 if type_kind == rustpp.TYPE_KIND_STD_VEC:
113 return RustStdVecPrinter(val)
115 if type_kind == rustpp.TYPE_KIND_STD_STRING:
116 return RustStdStringPrinter(val)
118 if type_kind == rustpp.TYPE_KIND_TUPLE:
119 return RustStructPrinter(val,
120 omit_first_field = False,
121 omit_type_name = True,
122 is_tuple_like = True)
124 if type_kind == rustpp.TYPE_KIND_TUPLE_STRUCT:
125 return RustStructPrinter(val,
126 omit_first_field = False,
127 omit_type_name = False,
128 is_tuple_like = True)
130 if type_kind == rustpp.TYPE_KIND_CSTYLE_VARIANT:
131 return RustCStyleVariantPrinter(val.get_child_at_index(0))
133 if type_kind == rustpp.TYPE_KIND_TUPLE_VARIANT:
134 return RustStructPrinter(val,
135 omit_first_field = True,
136 omit_type_name = False,
137 is_tuple_like = True)
139 if type_kind == rustpp.TYPE_KIND_SINGLETON_ENUM:
140 variant = get_field_at_index(gdb_val, 0)
141 return rust_pretty_printer_lookup_function(gdb_val[variant])
143 if type_kind == rustpp.TYPE_KIND_REGULAR_ENUM:
144 # This is a regular enum, extract the discriminant
145 discriminant_val = rustpp.get_discriminant_value_as_integer(val)
146 variant = get_field_at_index(gdb_val, discriminant_val)
147 return rust_pretty_printer_lookup_function(gdb_val[variant])
149 if type_kind == rustpp.TYPE_KIND_COMPRESSED_ENUM:
150 encoded_enum_info = rustpp.EncodedEnumInfo(val)
151 if encoded_enum_info.is_null_variant():
152 return IdentityPrinter(encoded_enum_info.get_null_variant_name())
154 non_null_val = encoded_enum_info.get_non_null_variant_val()
155 return rust_pretty_printer_lookup_function(non_null_val.get_wrapped_value())
157 # No pretty printer has been found
161 #=------------------------------------------------------------------------------
162 # Pretty Printer Classes
163 #=------------------------------------------------------------------------------
164 class RustStructPrinter:
165 def __init__(self, val, omit_first_field, omit_type_name, is_tuple_like):
167 self.__omit_first_field = omit_first_field
168 self.__omit_type_name = omit_type_name
169 self.__is_tuple_like = is_tuple_like
172 if self.__omit_type_name:
174 return self.__val.type.get_unqualified_type_name()
178 wrapped_value = self.__val.get_wrapped_value()
180 for field in self.__val.type.get_fields():
181 field_value = wrapped_value[field.name]
182 if self.__is_tuple_like:
183 cs.append(("", field_value))
185 cs.append((field.name, field_value))
187 if self.__omit_first_field:
192 def display_hint(self):
193 if self.__is_tuple_like:
199 class RustSlicePrinter:
200 def __init__(self, val):
203 def display_hint(self):
207 (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
208 return (self.__val.type.get_unqualified_type_name() +
209 ("(len: %i)" % length))
213 (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
214 assert data_ptr.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_PTR
215 raw_ptr = data_ptr.get_wrapped_value()
217 for index in range(0, length):
218 cs.append((str(index), (raw_ptr + index).dereference()))
223 class RustStringSlicePrinter:
224 def __init__(self, val):
228 (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
229 raw_ptr = data_ptr.get_wrapped_value()
230 return '"%s"' % raw_ptr.string(encoding="utf-8", length=length)
233 class RustStdVecPrinter:
234 def __init__(self, val):
237 def display_hint(self):
241 (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(self.__val)
242 return (self.__val.type.get_unqualified_type_name() +
243 ("(len: %i, cap: %i)" % (length, cap)))
247 (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(self.__val)
248 gdb_ptr = data_ptr.get_wrapped_value()
249 for index in range(0, length):
250 cs.append((str(index), (gdb_ptr + index).dereference()))
254 class RustStdStringPrinter:
255 def __init__(self, val):
259 vec = self.__val.get_child_at_index(0)
260 (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(vec)
261 return '"%s"' % data_ptr.get_wrapped_value().string(encoding="utf-8",
265 class RustCStyleVariantPrinter:
266 def __init__(self, val):
267 assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_ENUM
271 return str(self.__val.get_wrapped_value())
274 class IdentityPrinter:
275 def __init__(self, string):
282 def get_field_at_index(gdb_val, index):
284 for field in gdb_val.type.fields():