aboutsummaryrefslogtreecommitdiff
path: root/tools/zig-gdb.py
blob: d9921bcb3b78e3280e043c05de495697bd6bfe6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# pretty printing for stage1
# put "source /path/to/zig-gdb.py" in ~/.gdbinit to load it automatically

import gdb.printing

class ZigListPrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        return '%s of length %d, capacity %d' % (self.val.type.name, int(self.val['length']), int(self.val['capacity']))

    def children(self):
        def it(ziglist):
            for i in range(int(ziglist.val['length'])):
                item = ziglist.val['items'] + i
                yield ('[%d]' % i, item.dereference())
        return it(self)

    def display_hint(self):
        return 'array'

# handle both Buf and ZigList<char> because Buf* doesn't work otherwise (gdb bug?)
class BufPrinter:
    def __init__(self, val):
        self.val = val['list'] if val.type.name == 'Buf' else val

    def to_string(self):
        return self.val['items'].string(length=int(self.val['length']))

    def display_hint(self):
        return 'string'

pp = gdb.printing.RegexpCollectionPrettyPrinter('Zig stage1 compiler')
pp.add_printer('Buf', '^Buf$', BufPrinter)
pp.add_printer('ZigList<char>', '^ZigList<char>$', BufPrinter)
pp.add_printer('ZigList', '^ZigList<.*>$', ZigListPrinter)

gdb.printing.register_pretty_printer(gdb.current_objfile(), pp)