| ... | ... | @@ -0,0 +1,39 @@ |
| 1 | # pretty printing for stage1 |
| 2 | # put "source /path/to/zig-gdb.py" in ~/.gdbinit to load it automatically |
| 3 | |
| 4 | import gdb.printing |
| 5 | |
| 6 | class ZigListPrinter: |
| 7 | def __init__(self, val): |
| 8 | self.val = val |
| 9 | |
| 10 | def to_string(self): |
| 11 | return '%s of length %d, capacity %d' % (self.val.type.name, int(self.val['length']), int(self.val['capacity'])) |
| 12 | |
| 13 | def children(self): |
| 14 | def it(ziglist): |
| 15 | for i in range(int(ziglist.val['length'])): |
| 16 | item = ziglist.val['items'] + i |
| 17 | yield ('[%d]' % i, item.dereference()) |
| 18 | return it(self) |
| 19 | |
| 20 | def display_hint(self): |
| 21 | return 'array' |
| 22 | |
| 23 | # handle both Buf and ZigList<char> because Buf* doesn't work otherwise (gdb bug?) |
| 24 | class BufPrinter: |
| 25 | def __init__(self, val): |
| 26 | self.val = val['list'] if val.type.name == 'Buf' else val |
| 27 | |
| 28 | def to_string(self): |
| 29 | return self.val['items'].string(length=int(self.val['length'])) |
| 30 | |
| 31 | def display_hint(self): |
| 32 | return 'string' |
| 33 | |
| 34 | pp = gdb.printing.RegexpCollectionPrettyPrinter('zig') |
| 35 | pp.add_printer('Buf', '^Buf$', BufPrinter) |
| 36 | pp.add_printer('ZigList<char>', '^ZigList<char>$', BufPrinter) |
| 37 | pp.add_printer('ZigList', '^ZigList<.*>$', ZigListPrinter) |
| 38 | |
| 39 | gdb.printing.register_pretty_printer(gdb.current_objfile(), pp) |