2022-03-16 17:13:10 +00:00
|
|
|
# pretty printing for the language.
|
|
|
|
# put "source /path/to/zig_gdb_pretty_printers.py" in ~/.gdbinit to load it automatically.
|
2020-11-22 23:19:18 +00:00
|
|
|
import gdb.printing
|
|
|
|
|
2023-03-24 00:38:02 +00:00
|
|
|
|
2020-11-22 23:19:18 +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:
|
2020-11-22 23:19:18 +00:00
|
|
|
return None
|
2023-03-24 00:38:02 +00:00
|
|
|
if tag == '[]u8':
|
2020-11-22 23:19:18 +00:00
|
|
|
return StringPrinter(val)
|
2023-03-24 00:38:02 +00:00
|
|
|
if tag.startswith('[]'):
|
2020-11-22 23:19:18 +00:00
|
|
|
return SlicePrinter(val)
|
2023-03-24 00:38:02 +00:00
|
|
|
if tag.startswith('?'):
|
2020-11-22 23:19:18 +00:00
|
|
|
return OptionalPrinter(val)
|
|
|
|
return None
|
|
|
|
|
2023-03-24 00:38:02 +00:00
|
|
|
|
2020-11-22 23:19:18 +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
|
|
|
|
2020-11-22 23:19:18 +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
|
|
|
|
2020-11-22 23:19:18 +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']
|
2020-11-22 23:19:18 +00:00
|
|
|
else:
|
|
|
|
return 'null'
|
|
|
|
|
|
|
|
|
|
|
|
gdb.printing.register_pretty_printer(gdb.current_objfile(), ZigPrettyPrinter())
|