|
| 1 | +# pretty printing for stage 2. |
| 2 | +# put "source /path/to/stage2_gdb_pretty_printers.py" in ~/.gdbinit to load it automatically. |
| 3 | +import re |
| 4 | +import gdb.printing |
| 5 | + |
| 6 | +class TypePrinter: |
| 7 | + no_payload_count = 4096 |
| 8 | + |
| 9 | + # Keep in sync with src/type.zig |
| 10 | + # Types which have no payload do not need to be entered here. |
| 11 | + payload_type_names = { |
| 12 | + 'array_u8': 'type.Len', |
| 13 | + 'array_u8_sentinel_0': 'Len', |
| 14 | + |
| 15 | + 'single_const_pointer': 'ElemType', |
| 16 | + 'single_mut_pointer': 'ElemType', |
| 17 | + 'many_const_pointer': 'ElemType', |
| 18 | + 'many_mut_pointer': 'ElemType', |
| 19 | + 'c_const_pointer': 'ElemType', |
| 20 | + 'c_mut_pointer': 'ElemType', |
| 21 | + 'const_slice': 'ElemType', |
| 22 | + 'mut_slice': 'ElemType', |
| 23 | + 'optional': 'ElemType', |
| 24 | + 'optional_single_mut_pointer': 'ElemType', |
| 25 | + 'optional_single_const_pointer': 'ElemType', |
| 26 | + 'anyframe_T': 'ElemType', |
| 27 | + |
| 28 | + 'int_signed': 'Bits', |
| 29 | + 'int_unsigned': 'Bits', |
| 30 | + |
| 31 | + 'error_set': 'ErrorSet', |
| 32 | + 'error_set_inferred': 'ErrorSetInferred', |
| 33 | + 'error_set_merged': 'ErrorSetMerged', |
| 34 | + |
| 35 | + 'array': 'Array', |
| 36 | + 'vector': 'Array', |
| 37 | + |
| 38 | + 'array_sentinel': 'ArraySentinel', |
| 39 | + 'pointer': 'Pointer', |
| 40 | + 'function': 'Function', |
| 41 | + 'error_union': 'ErrorUnion', |
| 42 | + 'error_set_single': 'Name', |
| 43 | + 'opaque': 'Opaque', |
| 44 | + 'struct': 'Struct', |
| 45 | + 'union': 'Union', |
| 46 | + 'union_tagged': 'Union', |
| 47 | + 'enum_full, .enum_nonexhaustive': 'EnumFull', |
| 48 | + 'enum_simple': 'EnumSimple', |
| 49 | + 'enum_numbered': 'EnumNumbered', |
| 50 | + 'empty_struct': 'ContainerScope', |
| 51 | + 'tuple': 'Tuple', |
| 52 | + 'anon_struct': 'AnonStruct', |
| 53 | + } |
| 54 | + |
| 55 | + def __init__(self, val): |
| 56 | + self.val = val |
| 57 | + |
| 58 | + def tag(self): |
| 59 | + tag_if_small_enough = self.val['tag_if_small_enough'] |
| 60 | + tag_type = tag_if_small_enough.type |
| 61 | + |
| 62 | + if tag_if_small_enough < TypePrinter.no_payload_count: |
| 63 | + return tag_if_small_enough |
| 64 | + else: |
| 65 | + return self.val['ptr_otherwise'].dereference()['tag'] |
| 66 | + |
| 67 | + def payload_type(self): |
| 68 | + tag = self.tag() |
| 69 | + if tag is None: |
| 70 | + return None |
| 71 | + |
| 72 | + type_name = TypePrinter.payload_type_names.get(str(tag)) |
| 73 | + if type_name is None: |
| 74 | + return None |
| 75 | + return gdb.lookup_type('struct type.%s' % type_name) |
| 76 | + |
| 77 | + def to_string(self): |
| 78 | + tag = self.tag() |
| 79 | + if tag is None: |
| 80 | + return '(invalid type)' |
| 81 | + if self.val['tag_if_small_enough'] < TypePrinter.no_payload_count: |
| 82 | + return '.%s' % str(tag) |
| 83 | + return None |
| 84 | + |
| 85 | + def children(self): |
| 86 | + if self.val['tag_if_small_enough'] < TypePrinter.no_payload_count: |
| 87 | + return |
| 88 | + |
| 89 | + yield ('tag', '.%s' % str(self.tag())) |
| 90 | + |
| 91 | + payload_type = self.payload_type() |
| 92 | + if payload_type is not None: |
| 93 | + yield ('payload', self.val['ptr_otherwise'].cast(payload_type.pointer()).dereference()['data']) |
| 94 | + |
| 95 | +class ValuePrinter: |
| 96 | + no_payload_count = 4096 |
| 97 | + |
| 98 | + # Keep in sync with src/value.zig |
| 99 | + # Values which have no payload do not need to be entered here. |
| 100 | + payload_type_names = { |
| 101 | + 'big_int_positive': 'BigInt', |
| 102 | + 'big_int_negative': 'BigInt', |
| 103 | + |
| 104 | + 'extern_fn': 'ExternFn', |
| 105 | + |
| 106 | + 'decl_ref': 'Decl', |
| 107 | + |
| 108 | + 'repeated': 'SubValue', |
| 109 | + 'eu_payload': 'SubValue', |
| 110 | + 'opt_payload': 'SubValue', |
| 111 | + 'empty_array_sentinel': 'SubValue', |
| 112 | + |
| 113 | + 'eu_payload_ptr': 'PayloadPtr', |
| 114 | + 'opt_payload_ptr': 'PayloadPtr', |
| 115 | + |
| 116 | + 'bytes': 'Bytes', |
| 117 | + 'enum_literal': 'Bytes', |
| 118 | + |
| 119 | + 'slice': 'Slice', |
| 120 | + |
| 121 | + 'enum_field_index': 'U32', |
| 122 | + |
| 123 | + 'ty': 'Ty', |
| 124 | + 'int_type': 'IntType', |
| 125 | + 'int_u64': 'U64', |
| 126 | + 'int_i64': 'I64', |
| 127 | + 'function': 'Function', |
| 128 | + 'variable': 'Variable', |
| 129 | + 'decl_ref_mut': 'DeclRefMut', |
| 130 | + 'elem_ptr': 'ElemPtr', |
| 131 | + 'field_ptr': 'FieldPtr', |
| 132 | + 'float_16': 'Float_16', |
| 133 | + 'float_32': 'Float_32', |
| 134 | + 'float_64': 'Float_64', |
| 135 | + 'float_80': 'Float_80', |
| 136 | + 'float_128': 'Float_128', |
| 137 | + 'error': 'Error', |
| 138 | + 'inferred_alloc': 'InferredAlloc', |
| 139 | + 'inferred_alloc_comptime': 'InferredAllocComptime', |
| 140 | + 'aggregate': 'Aggregate', |
| 141 | + 'union': 'Union', |
| 142 | + 'bound_fn': 'BoundFn', |
| 143 | + } |
| 144 | + |
| 145 | + def __init__(self, val): |
| 146 | + self.val = val |
| 147 | + |
| 148 | + def tag(self): |
| 149 | + tag_if_small_enough = self.val['tag_if_small_enough'] |
| 150 | + tag_type = tag_if_small_enough.type |
| 151 | + |
| 152 | + if tag_if_small_enough < ValuePrinter.no_payload_count: |
| 153 | + return tag_if_small_enough |
| 154 | + else: |
| 155 | + return self.val['ptr_otherwise'].dereference()['tag'] |
| 156 | + |
| 157 | + def payload_type(self): |
| 158 | + tag = self.tag() |
| 159 | + if tag is None: |
| 160 | + return None |
| 161 | + |
| 162 | + type_name = ValuePrinter.payload_type_names.get(str(tag)) |
| 163 | + if type_name is None: |
| 164 | + return None |
| 165 | + return gdb.lookup_type('struct value.%s' % type_name) |
| 166 | + |
| 167 | + def to_string(self): |
| 168 | + tag = self.tag() |
| 169 | + if tag is None: |
| 170 | + return '(invalid value)' |
| 171 | + if self.val['tag_if_small_enough'] < ValuePrinter.no_payload_count: |
| 172 | + return '.%s' % str(tag) |
| 173 | + return None |
| 174 | + |
| 175 | + def children(self): |
| 176 | + if self.val['tag_if_small_enough'] < ValuePrinter.no_payload_count: |
| 177 | + return |
| 178 | + |
| 179 | + yield ('tag', '.%s' % str(self.tag())) |
| 180 | + |
| 181 | + payload_type = self.payload_type() |
| 182 | + if payload_type is not None: |
| 183 | + yield ('payload', self.val['ptr_otherwise'].cast(payload_type.pointer()).dereference()['data']) |
| 184 | + |
| 185 | +pp = gdb.printing.RegexpCollectionPrettyPrinter('Zig stage2 compiler') |
| 186 | +pp.add_printer('Type', r'^type\.Type$', TypePrinter) |
| 187 | +pp.add_printer('Value', r'^value\.Value$', ValuePrinter) |
| 188 | +gdb.printing.register_pretty_printer(gdb.current_objfile(), pp) |
| 189 | + |
0 commit comments