]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | # Copyright 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 lldb | |
9346a6ac | 12 | import re |
62682a34 | 13 | import debugger_pretty_printers_common as rustpp |
85aaf69f | 14 | |
62682a34 SL |
15 | #=============================================================================== |
16 | # LLDB Pretty Printing Module for Rust | |
17 | #=============================================================================== | |
1a4d82fc | 18 | |
62682a34 | 19 | class LldbType(rustpp.Type): |
1a4d82fc | 20 | |
62682a34 SL |
21 | def __init__(self, ty): |
22 | super(LldbType, self).__init__() | |
23 | self.ty = ty | |
24 | self.fields = None | |
1a4d82fc | 25 | |
62682a34 SL |
26 | def get_unqualified_type_name(self): |
27 | qualified_name = self.ty.GetName() | |
1a4d82fc | 28 | |
62682a34 SL |
29 | if qualified_name is None: |
30 | return qualified_name | |
1a4d82fc | 31 | |
9e0c209e | 32 | return rustpp.extract_type_name(qualified_name).replace("&'static ", "&") |
1a4d82fc | 33 | |
62682a34 SL |
34 | def get_dwarf_type_kind(self): |
35 | type_class = self.ty.GetTypeClass() | |
1a4d82fc | 36 | |
62682a34 SL |
37 | if type_class == lldb.eTypeClassStruct: |
38 | return rustpp.DWARF_TYPE_CODE_STRUCT | |
39 | ||
40 | if type_class == lldb.eTypeClassUnion: | |
41 | return rustpp.DWARF_TYPE_CODE_UNION | |
42 | ||
43 | if type_class == lldb.eTypeClassPointer: | |
44 | return rustpp.DWARF_TYPE_CODE_PTR | |
45 | ||
46 | if type_class == lldb.eTypeClassArray: | |
47 | return rustpp.DWARF_TYPE_CODE_ARRAY | |
48 | ||
49 | if type_class == lldb.eTypeClassEnumeration: | |
50 | return rustpp.DWARF_TYPE_CODE_ENUM | |
51 | ||
52 | return None | |
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 LldbValue(rustpp.Value): | |
66 | def __init__(self, lldb_val): | |
67 | ty = lldb_val.type | |
68 | wty = LldbType(ty) | |
69 | super(LldbValue, self).__init__(wty) | |
70 | self.lldb_val = lldb_val | |
71 | self.children = {} | |
72 | ||
73 | def get_child_at_index(self, index): | |
74 | child = self.children.get(index) | |
75 | if child is None: | |
76 | lldb_field = self.lldb_val.GetChildAtIndex(index) | |
77 | child = LldbValue(lldb_field) | |
78 | self.children[index] = child | |
79 | return child | |
80 | ||
81 | def as_integer(self): | |
82 | return self.lldb_val.GetValueAsUnsigned() | |
83 | ||
84 | def get_wrapped_value(self): | |
85 | return self.lldb_val | |
86 | ||
87 | ||
88 | def print_val(lldb_val, internal_dict): | |
89 | val = LldbValue(lldb_val) | |
90 | type_kind = val.type.get_type_kind() | |
1a4d82fc | 91 | |
62682a34 SL |
92 | if (type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT or |
93 | type_kind == rustpp.TYPE_KIND_EMPTY): | |
94 | return print_struct_val(val, | |
95 | internal_dict, | |
96 | omit_first_field = False, | |
97 | omit_type_name = False, | |
98 | is_tuple_like = False) | |
85aaf69f | 99 | |
62682a34 SL |
100 | if type_kind == rustpp.TYPE_KIND_STRUCT_VARIANT: |
101 | return print_struct_val(val, | |
102 | internal_dict, | |
103 | omit_first_field = True, | |
104 | omit_type_name = False, | |
105 | is_tuple_like = False) | |
106 | ||
107 | if type_kind == rustpp.TYPE_KIND_SLICE: | |
85aaf69f | 108 | return print_vec_slice_val(val, internal_dict) |
62682a34 SL |
109 | |
110 | if type_kind == rustpp.TYPE_KIND_STR_SLICE: | |
111 | return print_str_slice_val(val, internal_dict) | |
112 | ||
113 | if type_kind == rustpp.TYPE_KIND_STD_VEC: | |
c34b1796 | 114 | return print_std_vec_val(val, internal_dict) |
1a4d82fc | 115 | |
62682a34 SL |
116 | if type_kind == rustpp.TYPE_KIND_STD_STRING: |
117 | return print_std_string_val(val, internal_dict) | |
118 | ||
119 | if type_kind == rustpp.TYPE_KIND_TUPLE: | |
120 | return print_struct_val(val, | |
121 | internal_dict, | |
122 | omit_first_field = False, | |
123 | omit_type_name = True, | |
124 | is_tuple_like = True) | |
125 | ||
126 | if type_kind == rustpp.TYPE_KIND_TUPLE_STRUCT: | |
127 | return print_struct_val(val, | |
128 | internal_dict, | |
129 | omit_first_field = False, | |
130 | omit_type_name = False, | |
131 | is_tuple_like = True) | |
132 | ||
133 | if type_kind == rustpp.TYPE_KIND_CSTYLE_VARIANT: | |
134 | return val.type.get_unqualified_type_name() | |
135 | ||
136 | if type_kind == rustpp.TYPE_KIND_TUPLE_VARIANT: | |
137 | return print_struct_val(val, | |
138 | internal_dict, | |
139 | omit_first_field = True, | |
140 | omit_type_name = False, | |
141 | is_tuple_like = True) | |
142 | ||
143 | if type_kind == rustpp.TYPE_KIND_SINGLETON_ENUM: | |
144 | return print_val(lldb_val.GetChildAtIndex(0), internal_dict) | |
145 | ||
146 | if type_kind == rustpp.TYPE_KIND_PTR: | |
147 | return print_pointer_val(val, internal_dict) | |
148 | ||
149 | if type_kind == rustpp.TYPE_KIND_FIXED_SIZE_VEC: | |
150 | return print_fixed_size_vec_val(val, internal_dict) | |
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 | return print_val(lldb_val.GetChildAtIndex(discriminant_val), internal_dict) | |
156 | ||
157 | if type_kind == rustpp.TYPE_KIND_COMPRESSED_ENUM: | |
158 | encoded_enum_info = rustpp.EncodedEnumInfo(val) | |
159 | if encoded_enum_info.is_null_variant(): | |
160 | return encoded_enum_info.get_null_variant_name() | |
1a4d82fc | 161 | |
62682a34 SL |
162 | non_null_val = encoded_enum_info.get_non_null_variant_val() |
163 | return print_val(non_null_val.get_wrapped_value(), internal_dict) | |
164 | ||
165 | # No pretty printer has been found | |
166 | return lldb_val.GetValue() | |
167 | ||
168 | ||
169 | #=-------------------------------------------------------------------------------------------------- | |
170 | # Type-Specialized Printing Functions | |
171 | #=-------------------------------------------------------------------------------------------------- | |
172 | ||
173 | def print_struct_val(val, internal_dict, omit_first_field, omit_type_name, is_tuple_like): | |
9e0c209e | 174 | """ |
85aaf69f SL |
175 | Prints a struct, tuple, or tuple struct value with Rust syntax. |
176 | Ignores any fields before field_start_index. | |
9e0c209e | 177 | """ |
62682a34 | 178 | assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_STRUCT |
1a4d82fc | 179 | |
62682a34 SL |
180 | if omit_type_name: |
181 | type_name = "" | |
182 | else: | |
183 | type_name = val.type.get_unqualified_type_name() | |
1a4d82fc | 184 | |
9346a6ac | 185 | if is_tuple_like: |
85aaf69f SL |
186 | template = "%(type_name)s(%(body)s)" |
187 | separator = ", " | |
9346a6ac AL |
188 | else: |
189 | template = "%(type_name)s {\n%(body)s\n}" | |
190 | separator = ", \n" | |
85aaf69f | 191 | |
62682a34 | 192 | fields = val.type.get_fields() |
1a4d82fc | 193 | |
85aaf69f SL |
194 | def render_child(child_index): |
195 | this = "" | |
9346a6ac | 196 | if not is_tuple_like: |
62682a34 | 197 | field_name = fields[child_index].name |
85aaf69f | 198 | this += field_name + ": " |
1a4d82fc | 199 | |
62682a34 | 200 | field_val = val.get_child_at_index(child_index) |
c34b1796 | 201 | |
62682a34 SL |
202 | if not field_val.get_wrapped_value().IsValid(): |
203 | field = fields[child_index] | |
c34b1796 AL |
204 | # LLDB is not good at handling zero-sized values, so we have to help |
205 | # it a little | |
206 | if field.GetType().GetByteSize() == 0: | |
9e0c209e | 207 | return this + rustpp.extract_type_name(field.GetType().GetName()) |
c34b1796 AL |
208 | else: |
209 | return this + "<invalid value>" | |
210 | ||
62682a34 SL |
211 | return this + print_val(field_val.get_wrapped_value(), internal_dict) |
212 | ||
213 | if omit_first_field: | |
214 | field_start_index = 1 | |
215 | else: | |
216 | field_start_index = 0 | |
1a4d82fc | 217 | |
62682a34 | 218 | body = separator.join([render_child(idx) for idx in range(field_start_index, len(fields))]) |
85aaf69f SL |
219 | |
220 | return template % {"type_name": type_name, | |
221 | "body": body} | |
222 | ||
1a4d82fc | 223 | def print_pointer_val(val, internal_dict): |
9e0c209e | 224 | """Prints a pointer value with Rust syntax""" |
62682a34 | 225 | assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_PTR |
85aaf69f | 226 | sigil = "&" |
62682a34 SL |
227 | type_name = val.type.get_unqualified_type_name() |
228 | if type_name and type_name[0:1] in ["&", "*"]: | |
85aaf69f | 229 | sigil = type_name[0:1] |
1a4d82fc | 230 | |
62682a34 | 231 | return sigil + hex(val.as_integer()) |
1a4d82fc JJ |
232 | |
233 | ||
234 | def print_fixed_size_vec_val(val, internal_dict): | |
62682a34 SL |
235 | assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_ARRAY |
236 | lldb_val = val.get_wrapped_value() | |
1a4d82fc | 237 | |
85aaf69f | 238 | output = "[" |
1a4d82fc | 239 | |
62682a34 SL |
240 | for i in range(lldb_val.num_children): |
241 | output += print_val(lldb_val.GetChildAtIndex(i), internal_dict) | |
242 | if i != lldb_val.num_children - 1: | |
85aaf69f | 243 | output += ", " |
1a4d82fc | 244 | |
85aaf69f SL |
245 | output += "]" |
246 | return output | |
1a4d82fc JJ |
247 | |
248 | ||
c34b1796 | 249 | def print_vec_slice_val(val, internal_dict): |
62682a34 SL |
250 | (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(val) |
251 | return "&[%s]" % print_array_of_values(val.get_wrapped_value().GetName(), | |
252 | data_ptr, | |
c34b1796 AL |
253 | length, |
254 | internal_dict) | |
255 | ||
256 | ||
257 | def print_std_vec_val(val, internal_dict): | |
62682a34 SL |
258 | (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(val) |
259 | return "vec![%s]" % print_array_of_values(val.get_wrapped_value().GetName(), | |
260 | data_ptr, | |
c34b1796 AL |
261 | length, |
262 | internal_dict) | |
263 | ||
62682a34 SL |
264 | def print_str_slice_val(val, internal_dict): |
265 | (length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(val) | |
266 | return read_utf8_string(data_ptr, length) | |
267 | ||
268 | def print_std_string_val(val, internal_dict): | |
269 | vec = val.get_child_at_index(0) | |
270 | (length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(vec) | |
271 | return read_utf8_string(data_ptr, length) | |
272 | ||
1a4d82fc JJ |
273 | #=-------------------------------------------------------------------------------------------------- |
274 | # Helper Functions | |
275 | #=-------------------------------------------------------------------------------------------------- | |
276 | ||
c34b1796 | 277 | def print_array_of_values(array_name, data_ptr_val, length, internal_dict): |
9e0c209e SL |
278 | """Prints a contigous memory range, interpreting it as values of the |
279 | pointee-type of data_ptr_val.""" | |
c34b1796 | 280 | |
62682a34 SL |
281 | data_ptr_type = data_ptr_val.type |
282 | assert data_ptr_type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_PTR | |
c34b1796 | 283 | |
62682a34 | 284 | element_type = data_ptr_type.get_wrapped_value().GetPointeeType() |
c34b1796 AL |
285 | element_type_size = element_type.GetByteSize() |
286 | ||
62682a34 SL |
287 | start_address = data_ptr_val.as_integer() |
288 | raw_value = data_ptr_val.get_wrapped_value() | |
c34b1796 AL |
289 | |
290 | def render_element(i): | |
291 | address = start_address + i * element_type_size | |
62682a34 SL |
292 | element_val = raw_value.CreateValueFromAddress(array_name + ("[%s]" % i), |
293 | address, | |
294 | element_type) | |
c34b1796 AL |
295 | return print_val(element_val, internal_dict) |
296 | ||
297 | return ', '.join([render_element(i) for i in range(length)]) | |
62682a34 SL |
298 | |
299 | ||
300 | def read_utf8_string(ptr_val, byte_count): | |
301 | error = lldb.SBError() | |
302 | process = ptr_val.get_wrapped_value().GetProcess() | |
303 | data = process.ReadMemory(ptr_val.as_integer(), byte_count, error) | |
304 | if error.Success(): | |
305 | return '"%s"' % data.decode(encoding='UTF-8') | |
306 | else: | |
307 | return '<error: %s>' % error.GetCString() |