diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-05-22 12:00:20 +0430 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-05-22 13:21:00 +0430 |
commit | 37651a28e185857abfd5cab5139d36d9eee643bb (patch) | |
tree | b20ae0095bc90ffc2f8d52cf139703bfd947fb4b /Meta/serenity_gdb.py | |
parent | d1623350b5cbc5662ee6147e69d4b5e0b0cbbf6a (diff) | |
download | serenity-37651a28e185857abfd5cab5139d36d9eee643bb.zip |
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
Diffstat (limited to 'Meta/serenity_gdb.py')
-rw-r--r-- | Meta/serenity_gdb.py | 161 |
1 files changed, 118 insertions, 43 deletions
diff --git a/Meta/serenity_gdb.py b/Meta/serenity_gdb.py index 0aff2ad17b..7e3f7d1d7f 100644 --- a/Meta/serenity_gdb.py +++ b/Meta/serenity_gdb.py @@ -7,6 +7,53 @@ import gdb.types 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: def __init__(self, val): self.val = val @@ -14,6 +61,11 @@ class AKAtomic: def to_string(self): 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: def __init__(self, val): @@ -22,6 +74,11 @@ class AKDistinctNumeric: def to_string(self): 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: def __init__(self, val): @@ -30,6 +87,11 @@ class AKRefCounted: def to_string(self): 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: def __init__(self, val): @@ -42,6 +104,10 @@ class AKString: impl = AKRefPtr(self.val["m_impl"]).get_pointee().dereference() return AKStringImpl(impl).to_string() + @classmethod + def prettyprint_type(cls, type): + return 'AK::String' + class AKStringView: def __init__(self, val): @@ -55,6 +121,10 @@ class AKStringView: str_type = characters.type.target().array(self.val["m_length"]).pointer() return str(characters.cast(str_type).dereference()) + @classmethod + def prettyprint_type(cls, type): + return 'AK::StringView' + def get_field_unalloced(val, member, type): # 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"]) return get_field_unalloced(self.val, "m_inline_buffer", str_type) + @classmethod + def prettyprint_type(cls, type): + return 'AK::StringImpl' + class AKOwnPtr: def __init__(self, val): self.val = val def to_string(self): - return self.val.type.name + return AKOwnPtr.prettyprint_type(self.val.type) def children(self): 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: def __init__(self, val): self.val = val def to_string(self): - return self.val.type.name + return AKRefPtr.prettyprint_type(self.val.type) def get_pointee(self): inner_type = self.val.type.template_argument(0) @@ -101,13 +180,18 @@ class AKRefPtr: def children(self): 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: def __init__(self, val): self.val = val 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): vec_len = int(self.val["m_size"]) @@ -126,6 +210,11 @@ class AKVector: 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: def __init__(self, val): @@ -146,7 +235,7 @@ class AKHashMapPrettyPrinter: AKHashMapPrettyPrinter._iter_hashtable(table, lambda entry: cb(entry["key"], entry["value"])) def to_string(self): - return self.val.type.name + return AKHashMapPrettyPrinter.prettyprint_type(self.val.type) def children(self): elements = [] @@ -158,13 +247,19 @@ class AKHashMapPrettyPrinter: AKHashMapPrettyPrinter._iter_hashmap(self.val, cb) 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: def __init__(self, val): self.val = val def to_string(self): - return self.val.type.name + return AKSinglyLinkedList.prettyprint_type(self.val.type) def children(self): elements = [] @@ -176,13 +271,18 @@ class AKSinglyLinkedList: 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: def __init__(self, val): self.val = val def to_string(self): - return self.val.type.name + return AKInlineLinkedList.prettyprint_type(self.val.type) def children(self): 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))] + @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: def __init__(self, val): @@ -203,51 +308,21 @@ class VirtualAddress: def to_string(self): return self.val["m_address"] + @classmethod + def prettyprint_type(cls, type): + return 'VirtualAddress' + class SerenityPrettyPrinterLocator(gdb.printing.PrettyPrinter): def __init__(self): super(SerenityPrettyPrinterLocator, self).__init__("serenity_pretty_printers", []) - self.re = re.compile('^([^<]+)(<.*>)?$') - def __call__(self, val): type = gdb.types.get_basic_type(val.type) - typename = str(type.tag) - - match = self.re.match(typename) - if not match: + handler = handler_class_for_type(type) + if handler is UnhandledType: return None - - 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) + return handler(val) gdb.printing.register_pretty_printer(None, SerenityPrettyPrinterLocator(), replace=True) |