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 #===============================================================================
14 # GDB Pretty Printing Module for Rust
15 #===============================================================================
17 def register_printers(objfile):
18 "Registers Rust pretty printers for the given objfile"
19 objfile.pretty_printers.append(rust_pretty_printer_lookup_function)
21 def rust_pretty_printer_lookup_function(val):
22 "Returns the correct Rust pretty printer for the given value if there is one"
23 type_code = val.type.code
25 if type_code == gdb.TYPE_CODE_STRUCT:
26 struct_kind = classify_struct(val.type)
28 if struct_kind == STRUCT_KIND_STR_SLICE:
29 return RustStringSlicePrinter(val)
31 if struct_kind == STRUCT_KIND_TUPLE:
32 return RustTuplePrinter(val)
34 if struct_kind == STRUCT_KIND_TUPLE_STRUCT:
35 return RustTupleStructPrinter(val, False)
37 if struct_kind == STRUCT_KIND_CSTYLE_VARIANT:
38 return RustCStyleEnumPrinter(val[get_field_at_index(val, 0)])
40 if struct_kind == STRUCT_KIND_TUPLE_VARIANT:
41 return RustTupleStructPrinter(val, True)
43 if struct_kind == STRUCT_KIND_STRUCT_VARIANT:
44 return RustStructPrinter(val, True)
46 return RustStructPrinter(val, False)
49 if type_code == gdb.TYPE_CODE_UNION:
50 enum_members = list(val.type.fields())
51 enum_member_count = len(enum_members)
53 if enum_member_count == 0:
54 return RustStructPrinter(val, false)
56 if enum_member_count == 1:
57 if enum_members[0].name == None:
58 # This is a singleton enum
59 return rust_pretty_printer_lookup_function(val[enum_members[0]])
61 assert enum_members[0].name.startswith("RUST$ENCODED$ENUM$")
62 # This is a space-optimized enum
63 last_separator_index = enum_members[0].name.rfind("$")
64 second_last_separator_index = first_variant_name.rfind("$", 0, last_separator_index)
65 disr_field_index = first_variant_name[second_last_separator_index + 1 :
67 disr_field_index = int(disr_field_index)
69 sole_variant_val = val[enum_members[0]]
70 disr_field = get_field_at_index(sole_variant_val, disr_field_index)
71 discriminant = int(sole_variant_val[disr_field])
74 null_variant_name = first_variant_name[last_separator_index + 1:]
75 return IdentityPrinter(null_variant_name)
77 return rust_pretty_printer_lookup_function(sole_variant_val)
79 # This is a regular enum, extract the discriminant
80 discriminant_name, discriminant_val = extract_discriminant_value(val)
81 return rust_pretty_printer_lookup_function(val[enum_members[discriminant_val]])
83 # No pretty printer has been found
86 #=------------------------------------------------------------------------------
87 # Pretty Printer Classes
88 #=------------------------------------------------------------------------------
90 class RustStructPrinter:
91 def __init__(self, val, hide_first_field):
93 self.hide_first_field = hide_first_field
96 return self.val.type.tag
100 for field in self.val.type.fields():
101 field_name = field.name
102 # Normally the field name is used as a key to access the field value,
103 # because that's also supported in older versions of GDB...
104 field_key = field_name
105 if field_name == None:
107 # ... but for fields without a name (as in tuples), we have to fall back
108 # to the newer method of using the field object directly as key. In
109 # older versions of GDB, this will just fail.
111 name_value_tuple = ( field_name, self.val[field_key] )
112 cs.append( name_value_tuple )
114 if self.hide_first_field:
119 class RustTuplePrinter:
120 def __init__(self, val):
128 for field in self.val.type.fields():
129 cs.append( ("", self.val[field]) )
133 def display_hint(self):
136 class RustTupleStructPrinter:
137 def __init__(self, val, hide_first_field):
139 self.hide_first_field = hide_first_field
142 return self.val.type.tag
146 for field in self.val.type.fields():
147 cs.append( ("", self.val[field]) )
149 if self.hide_first_field:
154 def display_hint(self):
157 class RustStringSlicePrinter:
158 def __init__(self, val):
162 slice_byte_len = self.val["length"]
163 return '"%s"' % self.val["data_ptr"].string(encoding = "utf-8",
164 length = slice_byte_len)
166 class RustCStyleEnumPrinter:
167 def __init__(self, val):
168 assert val.type.code == gdb.TYPE_CODE_ENUM
174 class IdentityPrinter:
175 def __init__(self, string):
181 STRUCT_KIND_REGULAR_STRUCT = 0
182 STRUCT_KIND_TUPLE_STRUCT = 1
183 STRUCT_KIND_TUPLE = 2
184 STRUCT_KIND_TUPLE_VARIANT = 3
185 STRUCT_KIND_STRUCT_VARIANT = 4
186 STRUCT_KIND_CSTYLE_VARIANT = 5
187 STRUCT_KIND_STR_SLICE = 6
189 def classify_struct(type):
190 if type.tag == "&str":
191 return STRUCT_KIND_STR_SLICE
193 fields = list(type.fields())
194 field_count = len(fields)
197 return STRUCT_KIND_REGULAR_STRUCT
199 if fields[0].name == "RUST$ENUM$DISR":
201 return STRUCT_KIND_CSTYLE_VARIANT
202 elif fields[1].name == None:
203 return STRUCT_KIND_TUPLE_VARIANT
205 return STRUCT_KIND_STRUCT_VARIANT
207 if fields[0].name == None:
208 if type.tag.startswith("("):
209 return STRUCT_KIND_TUPLE
211 return STRUCT_KIND_TUPLE_STRUCT
213 return STRUCT_KIND_REGULAR_STRUCT
215 def extract_discriminant_value(enum_val):
216 assert enum_val.type.code == gdb.TYPE_CODE_UNION
217 for variant_descriptor in enum_val.type.fields():
218 variant_val = enum_val[variant_descriptor]
219 for field in variant_val.type.fields():
220 return (field.name, int(variant_val[field]))
222 def first_field(val):
223 for field in val.type.fields():
226 def get_field_at_index(val, index):
228 for field in val.type.fields():