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