mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 12:38:12 +00:00
Meta: Pretty-print the types in serenity_gdb.py
DistinctNumeric<T, ...> and some other types are very long, but the template arguments provide no particular value, so just don't show them
This commit is contained in:
parent
d1623350b5
commit
37651a28e1
1 changed files with 118 additions and 43 deletions
|
@ -7,6 +7,53 @@ import gdb.types
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def handler_class_for_type(type, re=re.compile('^([^<]+)(<.*>)?$')):
|
||||||
|
typename = str(type.tag)
|
||||||
|
|
||||||
|
match = re.match(typename)
|
||||||
|
if not match:
|
||||||
|
return None
|
||||||
|
|
||||||
|
klass = match.group(1)
|
||||||
|
|
||||||
|
if klass == 'AK::Atomic':
|
||||||
|
return AKAtomic
|
||||||
|
elif klass == 'AK::DistinctNumeric':
|
||||||
|
return AKDistinctNumeric
|
||||||
|
elif klass == 'AK::InlineLinkedList':
|
||||||
|
return AKInlineLinkedList
|
||||||
|
elif klass == 'AK::HashMap':
|
||||||
|
return AKHashMapPrettyPrinter
|
||||||
|
elif klass == 'AK::RefCounted':
|
||||||
|
return AKRefCounted
|
||||||
|
elif klass == 'AK::RefPtr':
|
||||||
|
return AKRefPtr
|
||||||
|
elif klass == 'AK::OwnPtr':
|
||||||
|
return AKOwnPtr
|
||||||
|
elif klass == 'AK::NonnullRefPtr':
|
||||||
|
return AKRefPtr
|
||||||
|
elif klass == 'AK::SinglyLinkedList':
|
||||||
|
return AKSinglyLinkedList
|
||||||
|
elif klass == 'AK::String':
|
||||||
|
return AKString
|
||||||
|
elif klass == 'AK::StringView':
|
||||||
|
return AKStringView
|
||||||
|
elif klass == 'AK::StringImpl':
|
||||||
|
return AKStringImpl
|
||||||
|
elif klass == 'AK::Vector':
|
||||||
|
return AKVector
|
||||||
|
elif klass == 'VirtualAddress':
|
||||||
|
return VirtualAddress
|
||||||
|
else:
|
||||||
|
return UnhandledType
|
||||||
|
|
||||||
|
|
||||||
|
class UnhandledType:
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
return type.name
|
||||||
|
|
||||||
|
|
||||||
class AKAtomic:
|
class AKAtomic:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
self.val = val
|
self.val = val
|
||||||
|
@ -14,6 +61,11 @@ class AKAtomic:
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return self.val["m_value"]
|
return self.val["m_value"]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
contained_type = type.template_argument(0)
|
||||||
|
return f'AK::Atomic<{handler_class_for_type(contained_type).prettyprint_type(contained_type)}>'
|
||||||
|
|
||||||
|
|
||||||
class AKDistinctNumeric:
|
class AKDistinctNumeric:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
|
@ -22,6 +74,11 @@ class AKDistinctNumeric:
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return self.val["m_value"]
|
return self.val["m_value"]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
contained_type = type.template_argument(0)
|
||||||
|
return f'AK::DistinctNumeric<{handler_class_for_type(contained_type).prettyprint_type(contained_type)}>'
|
||||||
|
|
||||||
|
|
||||||
class AKRefCounted:
|
class AKRefCounted:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
|
@ -30,6 +87,11 @@ class AKRefCounted:
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return self.val["m_ref_count"]
|
return self.val["m_ref_count"]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
contained_type = type.template_argument(0)
|
||||||
|
return f'AK::RefCounted<{handler_class_for_type(contained_type).prettyprint_type(contained_type)}>'
|
||||||
|
|
||||||
|
|
||||||
class AKString:
|
class AKString:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
|
@ -42,6 +104,10 @@ class AKString:
|
||||||
impl = AKRefPtr(self.val["m_impl"]).get_pointee().dereference()
|
impl = AKRefPtr(self.val["m_impl"]).get_pointee().dereference()
|
||||||
return AKStringImpl(impl).to_string()
|
return AKStringImpl(impl).to_string()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
return 'AK::String'
|
||||||
|
|
||||||
|
|
||||||
class AKStringView:
|
class AKStringView:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
|
@ -55,6 +121,10 @@ class AKStringView:
|
||||||
str_type = characters.type.target().array(self.val["m_length"]).pointer()
|
str_type = characters.type.target().array(self.val["m_length"]).pointer()
|
||||||
return str(characters.cast(str_type).dereference())
|
return str(characters.cast(str_type).dereference())
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
return 'AK::StringView'
|
||||||
|
|
||||||
|
|
||||||
def get_field_unalloced(val, member, type):
|
def get_field_unalloced(val, member, type):
|
||||||
# Trying to access a variable-length field seems to fail with
|
# Trying to access a variable-length field seems to fail with
|
||||||
|
@ -74,24 +144,33 @@ class AKStringImpl:
|
||||||
str_type = gdb.lookup_type("char").array(self.val["m_length"])
|
str_type = gdb.lookup_type("char").array(self.val["m_length"])
|
||||||
return get_field_unalloced(self.val, "m_inline_buffer", str_type)
|
return get_field_unalloced(self.val, "m_inline_buffer", str_type)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
return 'AK::StringImpl'
|
||||||
|
|
||||||
|
|
||||||
class AKOwnPtr:
|
class AKOwnPtr:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
self.val = val
|
self.val = val
|
||||||
|
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return self.val.type.name
|
return AKOwnPtr.prettyprint_type(self.val.type)
|
||||||
|
|
||||||
def children(self):
|
def children(self):
|
||||||
return [('*', self.val["m_ptr"])]
|
return [('*', self.val["m_ptr"])]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
contained_type = type.template_argument(0)
|
||||||
|
return f'AK::OwnPtr<{handler_class_for_type(contained_type).prettyprint_type(contained_type)}>'
|
||||||
|
|
||||||
|
|
||||||
class AKRefPtr:
|
class AKRefPtr:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
self.val = val
|
self.val = val
|
||||||
|
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return self.val.type.name
|
return AKRefPtr.prettyprint_type(self.val.type)
|
||||||
|
|
||||||
def get_pointee(self):
|
def get_pointee(self):
|
||||||
inner_type = self.val.type.template_argument(0)
|
inner_type = self.val.type.template_argument(0)
|
||||||
|
@ -101,13 +180,18 @@ class AKRefPtr:
|
||||||
def children(self):
|
def children(self):
|
||||||
return [('*', self.get_pointee())]
|
return [('*', self.get_pointee())]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
contained_type = type.template_argument(0)
|
||||||
|
return f'AK::RefPtr<{handler_class_for_type(contained_type).prettyprint_type(contained_type)}>'
|
||||||
|
|
||||||
|
|
||||||
class AKVector:
|
class AKVector:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
self.val = val
|
self.val = val
|
||||||
|
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return f'{self.val.type.name} of len {int(self.val["m_size"])}'
|
return f'{AKVector.prettyprint_type(self.val.type)} of len {int(self.val["m_size"])}'
|
||||||
|
|
||||||
def children(self):
|
def children(self):
|
||||||
vec_len = int(self.val["m_size"])
|
vec_len = int(self.val["m_size"])
|
||||||
|
@ -126,6 +210,11 @@ class AKVector:
|
||||||
|
|
||||||
return [(f"[{i}]", elements[i]) for i in range(vec_len)]
|
return [(f"[{i}]", elements[i]) for i in range(vec_len)]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
template_type = type.template_argument(0)
|
||||||
|
return f'AK::Vector<{handler_class_for_type(template_type).prettyprint_type(template_type)}>'
|
||||||
|
|
||||||
|
|
||||||
class AKHashMapPrettyPrinter:
|
class AKHashMapPrettyPrinter:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
|
@ -146,7 +235,7 @@ class AKHashMapPrettyPrinter:
|
||||||
AKHashMapPrettyPrinter._iter_hashtable(table, lambda entry: cb(entry["key"], entry["value"]))
|
AKHashMapPrettyPrinter._iter_hashtable(table, lambda entry: cb(entry["key"], entry["value"]))
|
||||||
|
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return self.val.type.name
|
return AKHashMapPrettyPrinter.prettyprint_type(self.val.type)
|
||||||
|
|
||||||
def children(self):
|
def children(self):
|
||||||
elements = []
|
elements = []
|
||||||
|
@ -158,13 +247,19 @@ class AKHashMapPrettyPrinter:
|
||||||
AKHashMapPrettyPrinter._iter_hashmap(self.val, cb)
|
AKHashMapPrettyPrinter._iter_hashmap(self.val, cb)
|
||||||
return elements
|
return elements
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
template_types = list(type.template_argument(i) for i in (0, 1))
|
||||||
|
key, value = list(handler_class_for_type(t).prettyprint_type(t) for t in template_types)
|
||||||
|
return f'AK::HashMap<{key}, {value}>'
|
||||||
|
|
||||||
|
|
||||||
class AKSinglyLinkedList:
|
class AKSinglyLinkedList:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
self.val = val
|
self.val = val
|
||||||
|
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return self.val.type.name
|
return AKSinglyLinkedList.prettyprint_type(self.val.type)
|
||||||
|
|
||||||
def children(self):
|
def children(self):
|
||||||
elements = []
|
elements = []
|
||||||
|
@ -176,13 +271,18 @@ class AKSinglyLinkedList:
|
||||||
|
|
||||||
return [(f"[{i}]", elements[i]) for i in range(len(elements))]
|
return [(f"[{i}]", elements[i]) for i in range(len(elements))]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
template_type = type.template_argument(0)
|
||||||
|
return f'AK::SinglyLinkedList<{handler_class_for_type(template_type).prettyprint_type(template_type)}>'
|
||||||
|
|
||||||
|
|
||||||
class AKInlineLinkedList:
|
class AKInlineLinkedList:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
self.val = val
|
self.val = val
|
||||||
|
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return self.val.type.name
|
return AKInlineLinkedList.prettyprint_type(self.val.type)
|
||||||
|
|
||||||
def children(self):
|
def children(self):
|
||||||
node_type_ptr = self.val.type.template_argument(0).pointer()
|
node_type_ptr = self.val.type.template_argument(0).pointer()
|
||||||
|
@ -195,6 +295,11 @@ class AKInlineLinkedList:
|
||||||
|
|
||||||
return [(f"[{i}]", elements[i].dereference()) for i in range(len(elements))]
|
return [(f"[{i}]", elements[i].dereference()) for i in range(len(elements))]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
template_type = type.template_argument(0)
|
||||||
|
return f'AK::InlineLinkedList<{handler_class_for_type(template_type).prettyprint_type(template_type)}>'
|
||||||
|
|
||||||
|
|
||||||
class VirtualAddress:
|
class VirtualAddress:
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
|
@ -203,51 +308,21 @@ class VirtualAddress:
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return self.val["m_address"]
|
return self.val["m_address"]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def prettyprint_type(cls, type):
|
||||||
|
return 'VirtualAddress'
|
||||||
|
|
||||||
|
|
||||||
class SerenityPrettyPrinterLocator(gdb.printing.PrettyPrinter):
|
class SerenityPrettyPrinterLocator(gdb.printing.PrettyPrinter):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(SerenityPrettyPrinterLocator, self).__init__("serenity_pretty_printers", [])
|
super(SerenityPrettyPrinterLocator, self).__init__("serenity_pretty_printers", [])
|
||||||
|
|
||||||
self.re = re.compile('^([^<]+)(<.*>)?$')
|
|
||||||
|
|
||||||
def __call__(self, val):
|
def __call__(self, val):
|
||||||
type = gdb.types.get_basic_type(val.type)
|
type = gdb.types.get_basic_type(val.type)
|
||||||
typename = str(type.tag)
|
handler = handler_class_for_type(type)
|
||||||
|
if handler is UnhandledType:
|
||||||
match = self.re.match(typename)
|
|
||||||
if not match:
|
|
||||||
return None
|
return None
|
||||||
|
return handler(val)
|
||||||
klass = match.group(1)
|
|
||||||
|
|
||||||
if klass == 'AK::Atomic':
|
|
||||||
return AKAtomic(val)
|
|
||||||
elif klass == 'AK::DistinctNumeric':
|
|
||||||
return AKDistinctNumeric(val)
|
|
||||||
elif klass == 'AK::InlineLinkedList':
|
|
||||||
return AKInlineLinkedList(val)
|
|
||||||
elif klass == 'AK::HashMap':
|
|
||||||
return AKHashMapPrettyPrinter(val)
|
|
||||||
elif klass == 'AK::RefCounted':
|
|
||||||
return AKRefCounted(val)
|
|
||||||
elif klass == 'AK::RefPtr':
|
|
||||||
return AKRefPtr(val)
|
|
||||||
elif klass == 'AK::OwnPtr':
|
|
||||||
return AKOwnPtr(val)
|
|
||||||
elif klass == 'AK::NonnullRefPtr':
|
|
||||||
return AKRefPtr(val)
|
|
||||||
elif klass == 'AK::SinglyLinkedList':
|
|
||||||
return AKSinglyLinkedList(val)
|
|
||||||
elif klass == 'AK::String':
|
|
||||||
return AKString(val)
|
|
||||||
elif klass == 'AK::StringView':
|
|
||||||
return AKStringView(val)
|
|
||||||
elif klass == 'AK::StringImpl':
|
|
||||||
return AKStringImpl(val)
|
|
||||||
elif klass == 'AK::Vector':
|
|
||||||
return AKVector(val)
|
|
||||||
elif klass == 'VirtualAddress':
|
|
||||||
return VirtualAddress(val)
|
|
||||||
|
|
||||||
|
|
||||||
gdb.printing.register_pretty_printer(None, SerenityPrettyPrinterLocator(), replace=True)
|
gdb.printing.register_pretty_printer(None, SerenityPrettyPrinterLocator(), replace=True)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue