2014-07-30 08:56:42 -05:00
|
|
|
# Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
# file at the top-level directory of this distribution and at
|
|
|
|
# http://rust-lang.org/COPYRIGHT.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
# option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
|
|
|
import gdb
|
2015-04-06 13:39:51 -05:00
|
|
|
import re
|
2015-05-09 09:48:36 -05:00
|
|
|
import debugger_pretty_printers_common as rustpp
|
2014-07-30 08:56:42 -05:00
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
# GDB Pretty Printing Module for Rust
|
|
|
|
#===============================================================================
|
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
class GdbType(rustpp.Type):
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def __init__(self, ty):
|
|
|
|
super(GdbType, self).__init__()
|
|
|
|
self.ty = ty
|
|
|
|
self.fields = None
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def get_unqualified_type_name(self):
|
|
|
|
tag = self.ty.tag
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if tag is None:
|
|
|
|
return tag
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
return tag.replace("&'static ", "&")
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def get_dwarf_type_kind(self):
|
|
|
|
if self.ty.code == gdb.TYPE_CODE_STRUCT:
|
|
|
|
return rustpp.DWARF_TYPE_CODE_STRUCT
|
2015-03-12 11:05:44 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if self.ty.code == gdb.TYPE_CODE_UNION:
|
|
|
|
return rustpp.DWARF_TYPE_CODE_UNION
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if self.ty.code == gdb.TYPE_CODE_PTR:
|
|
|
|
return rustpp.DWARF_TYPE_CODE_PTR
|
2015-03-12 11:05:44 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if self.ty.code == gdb.TYPE_CODE_ENUM:
|
|
|
|
return rustpp.DWARF_TYPE_CODE_ENUM
|
2015-03-12 11:05:44 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def get_fields(self):
|
|
|
|
assert ((self.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_STRUCT) or
|
|
|
|
(self.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_UNION))
|
|
|
|
if self.fields is None:
|
|
|
|
self.fields = list(self.ty.fields())
|
|
|
|
return self.fields
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def get_wrapped_value(self):
|
|
|
|
return self.ty
|
2015-01-27 01:50:24 -06:00
|
|
|
|
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
class GdbValue(rustpp.Value):
|
|
|
|
def __init__(self, gdb_val):
|
|
|
|
super(GdbValue, self).__init__(GdbType(gdb_val.type))
|
|
|
|
self.gdb_val = gdb_val
|
|
|
|
self.children = {}
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def get_child_at_index(self, index):
|
|
|
|
child = self.children.get(index)
|
|
|
|
if child is None:
|
|
|
|
gdb_field = get_field_at_index(self.gdb_val, index)
|
|
|
|
child = GdbValue(self.gdb_val[gdb_field])
|
|
|
|
self.children[index] = child
|
|
|
|
return child
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def as_integer(self):
|
|
|
|
return int(self.gdb_val)
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def get_wrapped_value(self):
|
|
|
|
return self.gdb_val
|
2015-01-27 01:50:24 -06:00
|
|
|
|
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def register_printers(objfile):
|
|
|
|
"""Registers Rust pretty printers for the given objfile"""
|
|
|
|
objfile.pretty_printers.append(rust_pretty_printer_lookup_function)
|
2015-01-27 01:50:24 -06:00
|
|
|
|
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def rust_pretty_printer_lookup_function(gdb_val):
|
|
|
|
"""
|
|
|
|
Returns the correct Rust pretty printer for the given value
|
|
|
|
if there is one
|
|
|
|
"""
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
val = GdbValue(gdb_val)
|
|
|
|
type_kind = val.type.get_type_kind()
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if (type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT or
|
|
|
|
type_kind == rustpp.TYPE_KIND_EMPTY):
|
|
|
|
return RustStructPrinter(val,
|
|
|
|
omit_first_field = False,
|
|
|
|
omit_type_name = False,
|
|
|
|
is_tuple_like = False)
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_STRUCT_VARIANT:
|
|
|
|
return RustStructPrinter(val,
|
|
|
|
omit_first_field = True,
|
|
|
|
omit_type_name = False,
|
|
|
|
is_tuple_like = False)
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_SLICE:
|
|
|
|
return RustSlicePrinter(val)
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_STR_SLICE:
|
|
|
|
return RustStringSlicePrinter(val)
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_STD_VEC:
|
|
|
|
return RustStdVecPrinter(val)
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_STD_STRING:
|
|
|
|
return RustStdStringPrinter(val)
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_TUPLE:
|
|
|
|
return RustStructPrinter(val,
|
|
|
|
omit_first_field = False,
|
|
|
|
omit_type_name = True,
|
|
|
|
is_tuple_like = True)
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_TUPLE_STRUCT:
|
|
|
|
return RustStructPrinter(val,
|
|
|
|
omit_first_field = False,
|
|
|
|
omit_type_name = False,
|
|
|
|
is_tuple_like = True)
|
2015-01-27 01:50:24 -06:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_CSTYLE_VARIANT:
|
|
|
|
return RustCStyleVariantPrinter(val.get_child_at_index(0))
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_TUPLE_VARIANT:
|
|
|
|
return RustStructPrinter(val,
|
|
|
|
omit_first_field = True,
|
|
|
|
omit_type_name = False,
|
|
|
|
is_tuple_like = True)
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_SINGLETON_ENUM:
|
|
|
|
variant = get_field_at_index(gdb_val, 0)
|
|
|
|
return rust_pretty_printer_lookup_function(gdb_val[variant])
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if type_kind == rustpp.TYPE_KIND_REGULAR_ENUM:
|
|
|
|
# This is a regular enum, extract the discriminant
|
|
|
|
discriminant_val = rustpp.get_discriminant_value_as_integer(val)
|
|
|
|
variant = get_field_at_index(gdb_val, discriminant_val)
|
|
|
|
return rust_pretty_printer_lookup_function(gdb_val[variant])
|
|
|
|
|
|
|
|
if type_kind == rustpp.TYPE_KIND_COMPRESSED_ENUM:
|
|
|
|
encoded_enum_info = rustpp.EncodedEnumInfo(val)
|
|
|
|
if encoded_enum_info.is_null_variant():
|
|
|
|
return IdentityPrinter(encoded_enum_info.get_null_variant_name())
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
non_null_val = encoded_enum_info.get_non_null_variant_val()
|
|
|
|
return rust_pretty_printer_lookup_function(non_null_val.get_wrapped_value())
|
|
|
|
|
|
|
|
# No pretty printer has been found
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
#=------------------------------------------------------------------------------
|
|
|
|
# Pretty Printer Classes
|
|
|
|
#=------------------------------------------------------------------------------
|
|
|
|
class RustStructPrinter:
|
|
|
|
def __init__(self, val, omit_first_field, omit_type_name, is_tuple_like):
|
|
|
|
self.__val = val
|
|
|
|
self.__omit_first_field = omit_first_field
|
|
|
|
self.__omit_type_name = omit_type_name
|
|
|
|
self.__is_tuple_like = is_tuple_like
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-01-27 01:50:24 -06:00
|
|
|
def to_string(self):
|
2015-05-09 09:48:36 -05:00
|
|
|
if self.__omit_type_name:
|
|
|
|
return None
|
|
|
|
return self.__val.type.get_unqualified_type_name()
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-01-27 01:50:24 -06:00
|
|
|
def children(self):
|
|
|
|
cs = []
|
2015-05-09 09:48:36 -05:00
|
|
|
wrapped_value = self.__val.get_wrapped_value()
|
|
|
|
|
|
|
|
for field in self.__val.type.get_fields():
|
|
|
|
field_value = wrapped_value[field.name]
|
|
|
|
if self.__is_tuple_like:
|
|
|
|
cs.append(("", field_value))
|
|
|
|
else:
|
|
|
|
cs.append((field.name, field_value))
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
if self.__omit_first_field:
|
2015-01-27 01:50:24 -06:00
|
|
|
cs = cs[1:]
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-01-27 01:50:24 -06:00
|
|
|
return cs
|
|
|
|
|
|
|
|
def display_hint(self):
|
2015-05-09 09:48:36 -05:00
|
|
|
if self.__is_tuple_like:
|
|
|
|
return "array"
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-03-12 11:05:44 -05:00
|
|
|
class RustSlicePrinter:
|
|
|
|
def __init__(self, val):
|
2015-05-09 09:48:36 -05:00
|
|
|
self.__val = val
|
2015-03-12 11:05:44 -05:00
|
|
|
|
|
|
|
def display_hint(self):
|
|
|
|
return "array"
|
|
|
|
|
|
|
|
def to_string(self):
|
2015-05-09 09:48:36 -05:00
|
|
|
(length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
|
|
|
|
return (self.__val.type.get_unqualified_type_name() +
|
|
|
|
("(len: %i)" % length))
|
2015-03-12 11:05:44 -05:00
|
|
|
|
|
|
|
def children(self):
|
|
|
|
cs = []
|
2015-05-09 09:48:36 -05:00
|
|
|
(length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
|
|
|
|
assert data_ptr.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_PTR
|
|
|
|
raw_ptr = data_ptr.get_wrapped_value()
|
2015-03-12 11:05:44 -05:00
|
|
|
|
|
|
|
for index in range(0, length):
|
2015-05-09 09:48:36 -05:00
|
|
|
cs.append((str(index), (raw_ptr + index).dereference()))
|
2015-03-12 11:05:44 -05:00
|
|
|
|
|
|
|
return cs
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
|
2014-07-30 08:56:42 -05:00
|
|
|
class RustStringSlicePrinter:
|
2015-01-27 01:50:24 -06:00
|
|
|
def __init__(self, val):
|
2015-05-09 09:48:36 -05:00
|
|
|
self.__val = val
|
2015-01-27 01:50:24 -06:00
|
|
|
|
|
|
|
def to_string(self):
|
2015-05-09 09:48:36 -05:00
|
|
|
(length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
|
|
|
|
raw_ptr = data_ptr.get_wrapped_value()
|
|
|
|
return '"%s"' % raw_ptr.string(encoding="utf-8", length=length)
|
|
|
|
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-03-12 11:05:44 -05:00
|
|
|
class RustStdVecPrinter:
|
|
|
|
def __init__(self, val):
|
2015-05-09 09:48:36 -05:00
|
|
|
self.__val = val
|
2015-03-12 11:05:44 -05:00
|
|
|
|
|
|
|
def display_hint(self):
|
|
|
|
return "array"
|
|
|
|
|
|
|
|
def to_string(self):
|
2015-05-09 09:48:36 -05:00
|
|
|
(length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(self.__val)
|
|
|
|
return (self.__val.type.get_unqualified_type_name() +
|
|
|
|
("(len: %i, cap: %i)" % (length, cap)))
|
2015-03-12 11:05:44 -05:00
|
|
|
|
|
|
|
def children(self):
|
|
|
|
cs = []
|
2015-05-09 09:48:36 -05:00
|
|
|
(length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(self.__val)
|
|
|
|
gdb_ptr = data_ptr.get_wrapped_value()
|
2015-03-12 11:05:44 -05:00
|
|
|
for index in range(0, length):
|
2015-05-09 09:48:36 -05:00
|
|
|
cs.append((str(index), (gdb_ptr + index).dereference()))
|
2015-03-12 11:05:44 -05:00
|
|
|
return cs
|
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
|
2015-03-12 11:05:44 -05:00
|
|
|
class RustStdStringPrinter:
|
|
|
|
def __init__(self, val):
|
2015-05-09 09:48:36 -05:00
|
|
|
self.__val = val
|
2015-03-12 11:05:44 -05:00
|
|
|
|
|
|
|
def to_string(self):
|
2015-05-09 09:48:36 -05:00
|
|
|
vec = self.__val.get_child_at_index(0)
|
|
|
|
(length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(vec)
|
|
|
|
return '"%s"' % data_ptr.get_wrapped_value().string(encoding="utf-8",
|
|
|
|
length=length)
|
2015-03-12 11:05:44 -05:00
|
|
|
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
class RustCStyleVariantPrinter:
|
2015-01-27 01:50:24 -06:00
|
|
|
def __init__(self, val):
|
2015-05-09 09:48:36 -05:00
|
|
|
assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_ENUM
|
|
|
|
self.__val = val
|
2015-01-27 01:50:24 -06:00
|
|
|
|
|
|
|
def to_string(self):
|
2015-05-09 09:48:36 -05:00
|
|
|
return str(self.__val.get_wrapped_value())
|
2014-07-30 08:56:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
class IdentityPrinter:
|
2015-01-27 01:50:24 -06:00
|
|
|
def __init__(self, string):
|
|
|
|
self.string = string
|
2014-07-30 08:56:42 -05:00
|
|
|
|
2015-01-27 01:50:24 -06:00
|
|
|
def to_string(self):
|
|
|
|
return self.string
|
2014-07-30 08:56:42 -05:00
|
|
|
|
|
|
|
|
2015-05-09 09:48:36 -05:00
|
|
|
def get_field_at_index(gdb_val, index):
|
2015-01-27 01:50:24 -06:00
|
|
|
i = 0
|
2015-05-09 09:48:36 -05:00
|
|
|
for field in gdb_val.type.fields():
|
2015-01-27 01:50:24 -06:00
|
|
|
if i == index:
|
|
|
|
return field
|
|
|
|
i += 1
|
|
|
|
return None
|