mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
gdb pretty printers for slices and optionals
This commit is contained in:
parent
49ca51fc3a
commit
0a6ba6f666
2 changed files with 65 additions and 1 deletions
|
|
@ -31,7 +31,7 @@ class BufPrinter:
|
||||||
def display_hint(self):
|
def display_hint(self):
|
||||||
return 'string'
|
return 'string'
|
||||||
|
|
||||||
pp = gdb.printing.RegexpCollectionPrettyPrinter('zig')
|
pp = gdb.printing.RegexpCollectionPrettyPrinter('Zig stage1 compiler')
|
||||||
pp.add_printer('Buf', '^Buf$', BufPrinter)
|
pp.add_printer('Buf', '^Buf$', BufPrinter)
|
||||||
pp.add_printer('ZigList<char>', '^ZigList<char>$', BufPrinter)
|
pp.add_printer('ZigList<char>', '^ZigList<char>$', BufPrinter)
|
||||||
pp.add_printer('ZigList', '^ZigList<.*>$', ZigListPrinter)
|
pp.add_printer('ZigList', '^ZigList<.*>$', ZigListPrinter)
|
||||||
|
|
|
||||||
64
tools/zig_gdb_pretty_printers.py
Normal file
64
tools/zig_gdb_pretty_printers.py
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
# gdb pretty printers for Zig language constructs
|
||||||
|
|
||||||
|
import gdb.printing
|
||||||
|
|
||||||
|
class ZigPrettyPrinter(gdb.printing.PrettyPrinter):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__('Zig')
|
||||||
|
|
||||||
|
def __call__(self, val):
|
||||||
|
tag = val.type.tag
|
||||||
|
if(tag is None):
|
||||||
|
return None
|
||||||
|
if(tag == '[]u8'):
|
||||||
|
return StringPrinter(val)
|
||||||
|
if(tag.startswith('[]')):
|
||||||
|
return SlicePrinter(val)
|
||||||
|
if(tag.startswith('?')):
|
||||||
|
return OptionalPrinter(val)
|
||||||
|
return None
|
||||||
|
|
||||||
|
class SlicePrinter:
|
||||||
|
def __init__(self, val):
|
||||||
|
self.val = val
|
||||||
|
|
||||||
|
def to_string(self):
|
||||||
|
return f"{self.val['len']} items at {self.val['ptr']}"
|
||||||
|
|
||||||
|
def children(self):
|
||||||
|
def it(val):
|
||||||
|
for i in range(int(val['len'])):
|
||||||
|
item = val['ptr'] + i
|
||||||
|
yield (f'[{i}]', item.dereference())
|
||||||
|
return it(self.val)
|
||||||
|
|
||||||
|
def display_hint(self):
|
||||||
|
return 'array'
|
||||||
|
|
||||||
|
class StringPrinter:
|
||||||
|
def __init__(self, val):
|
||||||
|
self.val = val
|
||||||
|
|
||||||
|
def to_string(self):
|
||||||
|
return self.val['ptr'].string(length=int(self.val['len']))
|
||||||
|
|
||||||
|
def display_hint(self):
|
||||||
|
return 'string'
|
||||||
|
|
||||||
|
class OptionalPrinter:
|
||||||
|
def __init__(self, val):
|
||||||
|
self.val = val
|
||||||
|
|
||||||
|
def to_string(self):
|
||||||
|
if(self.val['maybe']):
|
||||||
|
return None # printed by children()
|
||||||
|
else:
|
||||||
|
return 'null'
|
||||||
|
|
||||||
|
def children(self):
|
||||||
|
def it(val):
|
||||||
|
if(val['maybe']):
|
||||||
|
yield ('payload', val['val'])
|
||||||
|
return it(self.val)
|
||||||
|
|
||||||
|
gdb.printing.register_pretty_printer(gdb.current_objfile(), ZigPrettyPrinter())
|
||||||
Loading…
Add table
Reference in a new issue