gdb: add printer for selfhosted Type

This commit is contained in:
Robin Voetter 2022-03-16 17:40:21 +01:00
parent feb8981a95
commit 6830bcbb0a
2 changed files with 98 additions and 1 deletions

View file

@ -5092,6 +5092,7 @@ pub const Type = extern union {
pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1;
pub fn Type(comptime t: Tag) type {
// Keep in sync with tools/zig-gdb.py
return switch (t) {
.u1,
.u8,

View file

@ -1,8 +1,9 @@
# pretty printing for stage1, stage2 and the standard library
# put "source /path/to/zig-gdb.py" in ~/.gdbinit to load it automatically
import gdb.printing
import re
import gdb.printing
import gdb.types
class ZigListPrinter:
def __init__(self, val):
@ -184,6 +185,95 @@ class ArrayHashMapPrinter:
return 'map'
return 'array'
class TypePrinter:
no_payload_count = 4096
# Keep in sync with src/type.zig
# Types which have no payload do not need to be entered here.
payload_type_names = {
'array_u8': 'type.Len',
'array_u8_sentinel_0': 'Len',
'single_const_pointer': 'ElemType',
'single_mut_pointer': 'ElemType',
'many_const_pointer': 'ElemType',
'many_mut_pointer': 'ElemType',
'c_const_pointer': 'ElemType',
'c_mut_pointer': 'ElemType',
'const_slice': 'ElemType',
'mut_slice': 'ElemType',
'optional': 'ElemType',
'optional_single_mut_pointer': 'ElemType',
'optional_single_const_pointer': 'ElemType',
'anyframe_T': 'ElemType',
'int_signed': 'Bits',
'int_unsigned': 'Bits',
'error_set': 'ErrorSet',
'error_set_inferred': 'ErrorSetInferred',
'error_set_merged': 'ErrorSetMerged',
'array': 'Array',
'vector': 'Array',
'array_sentinel': 'ArraySentinel',
'pointer': 'Pointer',
'function': 'Function',
'error_union': 'ErrorUnion',
'error_set_single': 'Name',
'opaque': 'Opaque',
'struct': 'Struct',
'union': 'Union',
'union_tagged': 'Union',
'enum_full, .enum_nonexhaustive': 'EnumFull',
'enum_simple': 'EnumSimple',
'enum_numbered': 'EnumNumbered',
'empty_struct': 'ContainerScope',
'tuple': 'Tuple',
'anon_struct': 'AnonStruct',
}
def __init__(self, val):
self.val = val
def tag(self):
tag_if_small_enough = self.val['tag_if_small_enough']
tag_type = tag_if_small_enough.type
if tag_if_small_enough < TypePrinter.no_payload_count:
return tag_if_small_enough
else:
return self.val['ptr_otherwise'].dereference()['tag']
def payload_type(self):
tag = self.tag()
if tag is None:
return None
type_name = TypePrinter.payload_type_names.get(str(tag))
if type_name is None:
return None
return gdb.lookup_type('struct type.%s' % type_name)
def to_string(self):
tag = self.tag()
if tag is None:
return 'Type.(invalid type)'
if self.val['tag_if_small_enough'] < TypePrinter.no_payload_count:
return '.%s' % str(tag)
return None
def children(self):
if self.val['tag_if_small_enough'] < TypePrinter.no_payload_count:
return
yield ('tag', '.%s' % str(self.tag()))
payload_type = self.payload_type()
if payload_type is not None:
yield ('payload', self.val['ptr_otherwise'].cast(payload_type.pointer()).dereference()['data'])
pp1 = gdb.printing.RegexpCollectionPrettyPrinter('Zig stage1 compiler')
pp1.add_printer('Buf', '^Buf$', BufPrinter)
pp1.add_printer('ZigList<char>', '^ZigList<char>$', BufPrinter)
@ -193,6 +283,7 @@ gdb.printing.register_pretty_printer(gdb.current_objfile(), pp1)
pplang = gdb.printing.RegexpCollectionPrettyPrinter('Zig language')
pplang.add_printer('Slice', '^\[\]u8', SliceStringPrinter)
pplang.add_printer('Slice', '^\[\]', SlicePrinter)
gdb.printing.register_pretty_printer(gdb.current_objfile(), pplang)
ppstd = gdb.printing.RegexpCollectionPrettyPrinter('Zig standard library')
ppstd.add_printer('ArrayList', r'^std\.array_list\.ArrayListAligned(Unmanaged)?\(.*\)$', ArrayListPrinter)
@ -200,3 +291,8 @@ ppstd.add_printer('MultiArrayList', r'^std\.multi_array_list\.MultiArrayList\(.*
ppstd.add_printer('HashMap', r'^std\.hash_map\.HashMap(Unmanaged)?\(.*\)$', HashMapPrinter)
ppstd.add_printer('ArrayHashMap', r'^std\.array_hash_map\.ArrayHashMap(Unmanaged)?\(.*\)$', ArrayHashMapPrinter)
gdb.printing.register_pretty_printer(gdb.current_objfile(), ppstd)
pp2 = gdb.printing.RegexpCollectionPrettyPrinter('Zig stage2 compiler')
ppstd.add_printer('Type', r'^type\.Type$', TypePrinter)
gdb.printing.register_pretty_printer(gdb.current_objfile(), pp2)