Files
rust/src/etc/gdb_rust_pretty_printing.py

467 lines
15 KiB
Python
Raw Normal View History

import gdb
import re
2016-07-05 19:57:44 +02:00
import sys
import debugger_pretty_printers_common as rustpp
# We want a version of `range` which doesn't allocate an intermediate list,
# specifically it should use a lazy iterator. In Python 2 this was `xrange`, but
# if we're running with Python 3 then we need to use `range` instead.
if sys.version_info[0] >= 3:
2016-07-05 19:57:44 +02:00
xrange = range
2020-02-08 00:01:32 -05:00
rust_enabled = 'set language rust' in gdb.execute('complete set language ru', to_string=True)
# The btree pretty-printers fail in a confusing way unless
# https://sourceware.org/bugzilla/show_bug.cgi?id=21763 is fixed.
# This fix went in 8.1, so check for that.
# See https://github.com/rust-lang/rust/issues/56730
gdb_81 = False
_match = re.search('([0-9]+)\\.([0-9]+)', gdb.VERSION)
if _match:
if int(_match.group(1)) > 8 or (int(_match.group(1)) == 8 and int(_match.group(2)) >= 1):
gdb_81 = True
2020-02-08 00:01:32 -05:00
# ===============================================================================
# GDB Pretty Printing Module for Rust
2020-02-08 00:01:32 -05:00
# ===============================================================================
class GdbType(rustpp.Type):
2015-01-26 23:50:24 -08:00
def __init__(self, ty):
super(GdbType, self).__init__()
self.ty = ty
self.fields = None
2015-01-26 23:50:24 -08:00
def get_unqualified_type_name(self):
tag = self.ty.tag
if tag is None:
return tag
2015-01-26 23:50:24 -08:00
return rustpp.extract_type_name(tag).replace("&'static ", "&")
2015-01-26 23:50:24 -08:00
def get_dwarf_type_kind(self):
if self.ty.code == gdb.TYPE_CODE_STRUCT:
return rustpp.DWARF_TYPE_CODE_STRUCT
if self.ty.code == gdb.TYPE_CODE_UNION:
return rustpp.DWARF_TYPE_CODE_UNION
2015-01-26 23:50:24 -08:00
if self.ty.code == gdb.TYPE_CODE_PTR:
return rustpp.DWARF_TYPE_CODE_PTR
if self.ty.code == gdb.TYPE_CODE_ENUM:
return rustpp.DWARF_TYPE_CODE_ENUM
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-26 23:50:24 -08:00
def get_wrapped_value(self):
return self.ty
2015-01-26 23:50:24 -08: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-26 23:50:24 -08: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-26 23:50:24 -08:00
def as_integer(self):
if self.gdb_val.type.code == gdb.TYPE_CODE_PTR:
as_str = rustpp.compat_str(self.gdb_val).split()[0]
return int(as_str, 0)
return int(self.gdb_val)
2015-01-26 23:50:24 -08:00
def get_wrapped_value(self):
return self.gdb_val
2015-01-26 23:50:24 -08:00
def register_printers(objfile):
"""Registers Rust pretty printers for the given objfile"""
objfile.pretty_printers.append(rust_pretty_printer_lookup_function)
2015-01-26 23:50:24 -08:00
def rust_pretty_printer_lookup_function(gdb_val):
"""
Returns the correct Rust pretty printer for the given value
if there is one
"""
val = GdbValue(gdb_val)
type_kind = val.type.get_type_kind()
if type_kind == rustpp.TYPE_KIND_SLICE:
return RustSlicePrinter(val)
2015-01-26 23:50:24 -08:00
if type_kind == rustpp.TYPE_KIND_STD_VEC:
return RustStdVecPrinter(val)
2015-01-26 23:50:24 -08:00
if type_kind == rustpp.TYPE_KIND_STD_VECDEQUE:
return RustStdVecDequePrinter(val)
if type_kind == rustpp.TYPE_KIND_STD_BTREESET and gdb_81:
2018-08-06 23:00:19 +09:00
return RustStdBTreeSetPrinter(val)
if type_kind == rustpp.TYPE_KIND_STD_BTREEMAP and gdb_81:
2018-08-15 01:38:00 +09:00
return RustStdBTreeMapPrinter(val)
if type_kind == rustpp.TYPE_KIND_STD_STRING:
return RustStdStringPrinter(val)
2017-06-02 16:18:00 +03:00
if type_kind == rustpp.TYPE_KIND_OS_STRING:
return RustOsStringPrinter(val)
# Checks after this point should only be for "compiler" types --
# things that gdb's Rust language support knows about.
if rust_enabled:
return None
if type_kind == rustpp.TYPE_KIND_EMPTY:
return RustEmptyPrinter(val)
if type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT:
return RustStructPrinter(val,
2020-02-08 00:01:32 -05:00
omit_first_field=False,
omit_type_name=False,
is_tuple_like=False)
if type_kind == rustpp.TYPE_KIND_STRUCT_VARIANT:
return RustStructPrinter(val,
2020-02-08 00:01:32 -05:00
omit_first_field=True,
omit_type_name=False,
is_tuple_like=False)
if type_kind == rustpp.TYPE_KIND_STR_SLICE:
return RustStringSlicePrinter(val)
if type_kind == rustpp.TYPE_KIND_TUPLE:
return RustStructPrinter(val,
2020-02-08 00:01:32 -05:00
omit_first_field=False,
omit_type_name=True,
is_tuple_like=True)
if type_kind == rustpp.TYPE_KIND_TUPLE_STRUCT:
return RustStructPrinter(val,
2020-02-08 00:01:32 -05:00
omit_first_field=False,
omit_type_name=False,
is_tuple_like=True)
2015-01-26 23:50:24 -08:00
if type_kind == rustpp.TYPE_KIND_CSTYLE_VARIANT:
return RustCStyleVariantPrinter(val.get_child_at_index(0))
if type_kind == rustpp.TYPE_KIND_TUPLE_VARIANT:
return RustStructPrinter(val,
2020-02-08 00:01:32 -05:00
omit_first_field=True,
omit_type_name=False,
is_tuple_like=True)
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])
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())
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
2020-02-08 00:01:32 -05:00
# =------------------------------------------------------------------------------
# Pretty Printer Classes
2020-02-08 00:01:32 -05:00
# =------------------------------------------------------------------------------
class RustEmptyPrinter(object):
def __init__(self, val):
self.__val = val
def to_string(self):
return self.__val.type.get_unqualified_type_name()
2016-09-17 23:09:21 -07:00
class RustStructPrinter(object):
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
2015-01-26 23:50:24 -08:00
def to_string(self):
if self.__omit_type_name:
return None
return self.__val.type.get_unqualified_type_name()
2015-01-26 23:50:24 -08:00
def children(self):
cs = []
wrapped_value = self.__val.get_wrapped_value()
for number, field in enumerate(self.__val.type.get_fields()):
field_value = wrapped_value[field.name]
if self.__is_tuple_like:
cs.append((str(number), field_value))
else:
cs.append((field.name, field_value))
if self.__omit_first_field:
2015-01-26 23:50:24 -08:00
cs = cs[1:]
2015-01-26 23:50:24 -08:00
return cs
def display_hint(self):
if self.__is_tuple_like:
return "array"
else:
return ""
2016-09-17 23:09:21 -07:00
class RustSlicePrinter(object):
def __init__(self, val):
self.__val = val
2016-09-17 23:08:31 -07:00
@staticmethod
def display_hint():
return "array"
def to_string(self):
(length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
return (self.__val.type.get_unqualified_type_name() +
("(len: %i)" % length))
def children(self):
(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()
2016-07-05 19:57:44 +02:00
for index in xrange(0, length):
yield (str(index), (raw_ptr + index).dereference())
2016-09-17 23:09:21 -07:00
class RustStringSlicePrinter(object):
2015-01-26 23:50:24 -08:00
def __init__(self, val):
self.__val = val
2015-01-26 23:50:24 -08:00
def to_string(self):
(length, data_ptr) = rustpp.extract_length_and_ptr_from_slice(self.__val)
raw_ptr = data_ptr.get_wrapped_value()
return raw_ptr.lazy_string(encoding="utf-8", length=length)
def display_hint(self):
return "string"
2016-09-17 23:09:21 -07:00
class RustStdVecPrinter(object):
def __init__(self, val):
self.__val = val
2016-09-17 23:08:31 -07:00
@staticmethod
def display_hint():
return "array"
def to_string(self):
(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)))
def children(self):
saw_inaccessible = False
(length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(self.__val)
gdb_ptr = data_ptr.get_wrapped_value()
2016-07-05 19:57:44 +02:00
for index in xrange(0, length):
if saw_inaccessible:
return
try:
# rust-lang/rust#64343: passing deref expr to `str` allows
# catching exception on garbage pointer
str((gdb_ptr + index).dereference())
yield (str(index), (gdb_ptr + index).dereference())
except RuntimeError:
saw_inaccessible = True
yield (str(index), "inaccessible")
class RustStdVecDequePrinter(object):
def __init__(self, val):
self.__val = val
@staticmethod
def display_hint():
return "array"
def to_string(self):
2018-07-29 23:45:32 +09:00
(tail, head, data_ptr, cap) = \
rustpp.extract_tail_head_ptr_and_cap_from_std_vecdeque(self.__val)
if head >= tail:
size = head - tail
else:
size = cap + head - tail
return (self.__val.type.get_unqualified_type_name() +
("(len: %i, cap: %i)" % (size, cap)))
def children(self):
2018-07-29 23:45:32 +09:00
(tail, head, data_ptr, cap) = \
rustpp.extract_tail_head_ptr_and_cap_from_std_vecdeque(self.__val)
gdb_ptr = data_ptr.get_wrapped_value()
if head >= tail:
size = head - tail
else:
size = cap + head - tail
for index in xrange(0, size):
yield (str(index), (gdb_ptr + ((tail + index) % cap)).dereference())
# Yield each key (and optionally value) from a BoxedNode.
def children_of_node(boxed_node, height, want_values):
node_ptr = boxed_node['ptr']['pointer']
if height > 0:
type_name = str(node_ptr.type.target()).replace('LeafNode', 'InternalNode', 1)
node_type = gdb.lookup_type(type_name)
node_ptr = node_ptr.cast(node_type.pointer())
leaf = node_ptr['data']
else:
leaf = node_ptr.dereference()
2019-01-28 17:33:29 +01:00
keys = leaf['keys']
if want_values:
2019-01-28 17:33:29 +01:00
values = leaf['vals']
length = int(leaf['len'])
for i in xrange(0, length + 1):
if height > 0:
2019-01-28 17:33:29 +01:00
child_ptr = node_ptr['edges'][i]['value']['value']
for child in children_of_node(child_ptr, height - 1, want_values):
yield child
if i < length:
if want_values:
2019-01-28 17:33:29 +01:00
yield (keys[i]['value']['value'], values[i]['value']['value'])
else:
2019-01-28 17:33:29 +01:00
yield keys[i]['value']['value']
2020-02-08 00:01:32 -05:00
2018-08-06 23:00:19 +09:00
class RustStdBTreeSetPrinter(object):
def __init__(self, val):
self.__val = val
@staticmethod
def display_hint():
2018-08-13 23:01:48 +09:00
return "array"
2018-08-06 23:00:19 +09:00
def to_string(self):
return (self.__val.type.get_unqualified_type_name() +
("(len: %i)" % self.__val.get_wrapped_value()['map']['length']))
2018-08-06 23:00:19 +09:00
def children(self):
2020-03-18 15:29:05 -04:00
prev_idx = None
innermap = GdbValue(self.__val.get_wrapped_value()['map'])
if innermap.get_wrapped_value()['length'] > 0:
root = GdbValue(innermap.get_wrapped_value()['root'])
2020-03-22 20:10:48 +00:00
type_name = str(root.type.ty.name).replace('core::option::Option<', '', 1)[:-1]
2020-03-18 15:29:05 -04:00
root = root.get_wrapped_value().cast(gdb.lookup_type(type_name))
node_ptr = root['node']
i = 0
for child in children_of_node(node_ptr, root['height'], False):
yield (str(i), child)
i = i + 1
2018-08-06 23:00:19 +09:00
2018-08-15 01:38:00 +09:00
class RustStdBTreeMapPrinter(object):
def __init__(self, val):
self.__val = val
@staticmethod
def display_hint():
return "map"
def to_string(self):
return (self.__val.type.get_unqualified_type_name() +
("(len: %i)" % self.__val.get_wrapped_value()['length']))
2018-08-15 01:38:00 +09:00
def children(self):
2020-03-18 15:29:05 -04:00
if self.__val.get_wrapped_value()['length'] > 0:
root = GdbValue(self.__val.get_wrapped_value()['root'])
2020-03-22 20:10:48 +00:00
type_name = str(root.type.ty.name).replace('core::option::Option<', '', 1)[:-1]
2020-03-18 15:29:05 -04:00
root = root.get_wrapped_value().cast(gdb.lookup_type(type_name))
node_ptr = root['node']
i = 0
for child in children_of_node(node_ptr, root['height'], True):
yield (str(i), child[0])
yield (str(i), child[1])
i = i + 1
2018-08-15 01:38:00 +09:00
2016-09-17 23:09:21 -07:00
class RustStdStringPrinter(object):
def __init__(self, val):
self.__val = val
def to_string(self):
vec = self.__val.get_child_at_index(0)
(length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(vec)
return data_ptr.get_wrapped_value().lazy_string(encoding="utf-8",
length=length)
def display_hint(self):
return "string"
2018-08-15 01:38:00 +09:00
2017-06-02 16:18:00 +03:00
class RustOsStringPrinter(object):
def __init__(self, val):
self.__val = val
def to_string(self):
buf = self.__val.get_child_at_index(0)
vec = buf.get_child_at_index(0)
if vec.type.get_unqualified_type_name() == "Wtf8Buf":
vec = vec.get_child_at_index(0)
(length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(
vec)
return data_ptr.get_wrapped_value().lazy_string(length=length)
2017-06-02 16:18:00 +03:00
def display_hint(self):
return "string"
2017-06-02 16:18:00 +03:00
2020-02-08 00:01:32 -05:00
2016-09-17 23:09:21 -07:00
class RustCStyleVariantPrinter(object):
2015-01-26 23:50:24 -08:00
def __init__(self, val):
assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_ENUM
self.__val = val
2015-01-26 23:50:24 -08:00
def to_string(self):
return str(self.__val.get_wrapped_value())
2016-09-17 23:09:21 -07:00
class IdentityPrinter(object):
2015-01-26 23:50:24 -08:00
def __init__(self, string):
self.string = string
2015-01-26 23:50:24 -08:00
def to_string(self):
return self.string
def get_field_at_index(gdb_val, index):
2015-01-26 23:50:24 -08:00
i = 0
for field in gdb_val.type.fields():
2015-01-26 23:50:24 -08:00
if i == index:
return field
i += 1
return None