zig/tools/zig_gdb_pretty_printers.py

64 lines
1.5 KiB
Python
Raw Normal View History

# pretty printing for the language.
# put "source /path/to/zig_gdb_pretty_printers.py" in ~/.gdbinit to load it automatically.
import gdb.printing
2023-03-24 00:38:02 +00:00
class ZigPrettyPrinter(gdb.printing.PrettyPrinter):
def __init__(self):
super().__init__('Zig')
def __call__(self, val):
tag = val.type.tag
2023-03-24 00:38:02 +00:00
if tag is None:
return None
2023-03-24 00:38:02 +00:00
if tag == '[]u8':
return StringPrinter(val)
2023-03-24 00:38:02 +00:00
if tag.startswith('[]'):
return SlicePrinter(val)
2023-03-24 00:38:02 +00:00
if tag.startswith('?'):
return OptionalPrinter(val)
return None
2023-03-24 00:38:02 +00:00
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'
2023-03-24 00:38:02 +00:00
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'
2023-03-24 00:38:02 +00:00
class OptionalPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
2023-03-24 00:38:02 +00:00
if self.val['some']:
return self.val['data']
else:
return 'null'
gdb.printing.register_pretty_printer(gdb.current_objfile(), ZigPrettyPrinter())