]> git.lizzy.rs Git - rust.git/blob - src/etc/gdb_rust_pretty_printing.py
Changed issue number to 36105
[rust.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.major >= 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 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             return int(str(self.gdb_val), 0)
82         return int(self.gdb_val)
83
84     def get_wrapped_value(self):
85         return self.gdb_val
86
87
88 def register_printers(objfile):
89     """Registers Rust pretty printers for the given objfile"""
90     objfile.pretty_printers.append(rust_pretty_printer_lookup_function)
91
92
93 def rust_pretty_printer_lookup_function(gdb_val):
94     """
95     Returns the correct Rust pretty printer for the given value
96     if there is one
97     """
98
99     val = GdbValue(gdb_val)
100     type_kind = val.type.get_type_kind()
101
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)
108
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)
114
115     if type_kind == rustpp.TYPE_KIND_SLICE:
116         return RustSlicePrinter(val)
117
118     if type_kind == rustpp.TYPE_KIND_STR_SLICE:
119         return RustStringSlicePrinter(val)
120
121     if type_kind == rustpp.TYPE_KIND_STD_VEC:
122         return RustStdVecPrinter(val)
123
124     if type_kind == rustpp.TYPE_KIND_STD_STRING:
125         return RustStdStringPrinter(val)
126
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)
132
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)
138
139     if type_kind == rustpp.TYPE_KIND_CSTYLE_VARIANT:
140         return RustCStyleVariantPrinter(val.get_child_at_index(0))
141
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)
147
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])
151
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])
157
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())
162
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())
165
166     # No pretty printer has been found
167     return None
168
169
170 #=------------------------------------------------------------------------------
171 # Pretty Printer Classes
172 #=------------------------------------------------------------------------------
173 class RustStructPrinter:
174     def __init__(self, val, omit_first_field, omit_type_name, is_tuple_like):
175         self.__val = val
176         self.__omit_first_field = omit_first_field
177         self.__omit_type_name = omit_type_name
178         self.__is_tuple_like = is_tuple_like
179
180     def to_string(self):
181         if self.__omit_type_name:
182             return None
183         return self.__val.type.get_unqualified_type_name()
184
185     def children(self):
186         cs = []
187         wrapped_value = self.__val.get_wrapped_value()
188
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))
193             else:
194                 cs.append((field.name, field_value))
195
196         if self.__omit_first_field:
197             cs = cs[1:]
198
199         return cs
200
201     def display_hint(self):
202         if self.__is_tuple_like:
203             return "array"
204         else:
205             return ""
206
207
208 class RustSlicePrinter:
209     def __init__(self, val):
210         self.__val = val
211
212     def display_hint(self):
213         return "array"
214
215     def to_string(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))
219
220     def children(self):
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()
224
225         for index in xrange(0, length):
226             yield (str(index), (raw_ptr + index).dereference())
227
228
229 class RustStringSlicePrinter:
230     def __init__(self, val):
231         self.__val = val
232
233     def to_string(self):
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)
237
238
239 class RustStdVecPrinter:
240     def __init__(self, val):
241         self.__val = val
242
243     def display_hint(self):
244         return "array"
245
246     def to_string(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)))
250
251     def children(self):
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())
256
257
258 class RustStdStringPrinter:
259     def __init__(self, val):
260         self.__val = val
261
262     def to_string(self):
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",
266                                                             length=length)
267
268
269 class RustCStyleVariantPrinter:
270     def __init__(self, val):
271         assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_ENUM
272         self.__val = val
273
274     def to_string(self):
275         return str(self.__val.get_wrapped_value())
276
277
278 class IdentityPrinter:
279     def __init__(self, string):
280         self.string = string
281
282     def to_string(self):
283         return self.string
284
285
286 def get_field_at_index(gdb_val, index):
287     i = 0
288     for field in gdb_val.type.fields():
289         if i == index:
290             return field
291         i += 1
292     return None