| ... | ... | @@ -1,7 +1,8 @@ |
| 1 | | # pretty printing for stage1 |
| 1 | # pretty printing for stage1, stage2 and the standard library |
| 2 | 2 | # put "source /path/to/zig-gdb.py" in ~/.gdbinit to load it automatically |
| 3 | 3 | |
| 4 | 4 | import gdb.printing |
| 5 | import re |
| 5 | 6 | |
| 6 | 7 | class ZigListPrinter: |
| 7 | 8 | def __init__(self, val): |
| ... | ... | @@ -31,9 +32,59 @@ class BufPrinter: |
| 31 | 32 | def display_hint(self): |
| 32 | 33 | return 'string' |
| 33 | 34 | |
| 35 | # Handles both ArrayList and ArrayListUnmanaged. |
| 36 | class ArrayListPrinter: |
| 37 | def __init__(self, val): |
| 38 | self.val = val |
| 39 | |
| 40 | def to_string(self): |
| 41 | type = self.val.type.name[len('std.array_list.'):] |
| 42 | type = re.sub(r'ArrayListAligned(Unmanaged)?\((.*),null\)$', r'ArrayList\1(\2)', type) |
| 43 | return '%s of length %s, capacity %s' % (type, self.val['items']['len'], self.val['capacity']) |
| 44 | |
| 45 | def children(self): |
| 46 | for i in range(int(self.val['items']['len'])): |
| 47 | item = self.val['items']['ptr'] + i |
| 48 | yield ('[%d]' % i, item.dereference()) |
| 49 | |
| 50 | def display_hint(self): |
| 51 | return 'array' |
| 52 | |
| 53 | # Handles both HashMap and HashMapUnmanaged |
| 54 | class HashMapPrinter: |
| 55 | def __init__(self, val): |
| 56 | self.type = val.type |
| 57 | is_managed = re.search(r'^std\.hash_map\.HashMap\(', self.type.name) |
| 58 | self.val = val['unmanaged'] if is_managed else val |
| 59 | |
| 60 | def header(self): |
| 61 | (header_fn, _) = gdb.lookup_symbol('%s.header' % self.val.type.name) |
| 62 | header_ptr_type = header_fn.type.target() |
| 63 | return (self.val['metadata'].cast(header_ptr_type) - 1).dereference() |
| 64 | |
| 65 | def to_string(self): |
| 66 | type = self.type.name[len('std.hash_map.'):] |
| 67 | type = re.sub(r'^HashMap(Unmanaged)?\((.*),std.hash_map.AutoContext\(.*$', r'AutoHashMap\1(\2)', type) |
| 68 | return '%s of length %s, capacity %s' % (type, self.val['size'], self.header()['capacity']) |
| 69 | |
| 70 | def children(self): |
| 71 | hdr = self.header() |
| 72 | for i in range(int(hdr['capacity'])): |
| 73 | metadata = self.val['metadata'] + i |
| 74 | if metadata.dereference()['used'] == 1: |
| 75 | yield ('[%d]' % i, (hdr['keys'] + i).dereference()) |
| 76 | yield ('[%d]' % i, (hdr['values'] + i).dereference()) |
| 77 | |
| 78 | def display_hint(self): |
| 79 | return 'map' |
| 80 | |
| 34 | 81 | pp = gdb.printing.RegexpCollectionPrettyPrinter('Zig stage1 compiler') |
| 35 | 82 | pp.add_printer('Buf', '^Buf$', BufPrinter) |
| 36 | 83 | pp.add_printer('ZigList<char>', '^ZigList<char>$', BufPrinter) |
| 37 | 84 | pp.add_printer('ZigList', '^ZigList<.*>$', ZigListPrinter) |
| 38 | | |
| 39 | 85 | gdb.printing.register_pretty_printer(gdb.current_objfile(), pp) |
| 86 | |
| 87 | ppstd = gdb.printing.RegexpCollectionPrettyPrinter('Zig standard library') |
| 88 | ppstd.add_printer('ArrayList', r'^std\.array_list\.ArrayListAligned(Unmanaged)?\(.*\)$', ArrayListPrinter) |
| 89 | ppstd.add_printer('HashMap', r'^std\.hash_map\.HashMap(Unmanaged)?\(.*\)$', HashMapPrinter) |
| 90 | gdb.printing.register_pretty_printer(gdb.current_objfile(), ppstd) |