diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig index 164b81d651..af0ecc5993 100644 --- a/lib/std/hash_map.zig +++ b/lib/std/hash_map.zig @@ -1595,16 +1595,17 @@ pub fn HashMapUnmanaged( self.available = 0; } - /// This function is used in tools/zig-gdb.py to fetch the header type to facilitate - /// fancy debug printing for this type. - fn gdbHelper(self: *Self, hdr: *Header) void { + /// This function is used in the debugger pretty formatters in tools/ to fetch the + /// header type to facilitate fancy debug printing for this type. + fn dbHelper(self: *Self, hdr: *Header, entry: *Entry) void { _ = self; _ = hdr; + _ = entry; } comptime { if (builtin.mode == .Debug) { - _ = gdbHelper; + _ = dbHelper; } } }; diff --git a/lib/std/multi_array_list.zig b/lib/std/multi_array_list.zig index 56b36aaa81..6965205b1e 100644 --- a/lib/std/multi_array_list.zig +++ b/lib/std/multi_array_list.zig @@ -131,8 +131,8 @@ pub fn MultiArrayList(comptime S: type) type { .capacity = self.capacity, }; var ptr: [*]u8 = self.bytes; - for (sizes.bytes, 0..) |field_size, i| { - result.ptrs[sizes.fields[i]] = ptr; + for (sizes.bytes, sizes.fields) |field_size, i| { + result.ptrs[i] = ptr; ptr += field_size * self.capacity; } return result; @@ -446,16 +446,33 @@ pub fn MultiArrayList(comptime S: type) type { return meta.fieldInfo(S, field).type; } - /// This function is used in tools/zig-gdb.py to fetch the child type to facilitate - /// fancy debug printing for this type. - fn gdbHelper(self: *Self, child: *S) void { + const Entry = entry: { + var entry_fields: [fields.len]std.builtin.Type.StructField = undefined; + for (&entry_fields, sizes.fields) |*entry_field, i| entry_field.* = .{ + .name = fields[i].name ++ "_ptr", + .type = *fields[i].type, + .default_value = null, + .is_comptime = fields[i].is_comptime, + .alignment = fields[i].alignment, + }; + break :entry @Type(.{ .Struct = .{ + .layout = .Extern, + .fields = &entry_fields, + .decls = &.{}, + .is_tuple = false, + } }); + }; + /// This function is used in the debugger pretty formatters in tools/ to fetch the + /// child type to facilitate fancy debug printing for this type. + fn dbHelper(self: *Self, child: *S, entry: *Entry) void { _ = self; _ = child; + _ = entry; } comptime { if (builtin.mode == .Debug) { - _ = gdbHelper; + _ = dbHelper; } } }; diff --git a/src/type.zig b/src/type.zig index ec4db8689f..15525f14eb 100644 --- a/src/type.zig +++ b/src/type.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const Value = @import("value.zig").Value; const assert = std.debug.assert; const Allocator = std.mem.Allocator; @@ -6694,4 +6695,33 @@ pub const Type = extern union { /// This is only used for comptime asserts. Bump this number when you make a change /// to packed struct layout to find out all the places in the codebase you need to edit! pub const packed_struct_layout_version = 2; + + /// This function is used in the debugger pretty formatters in tools/ to fetch the + /// Tag to Payload mapping to facilitate fancy debug printing for this type. + fn dbHelper(self: *Type, tag_to_payload_map: *map: { + const tags = @typeInfo(Tag).Enum.fields; + var fields: [tags.len]std.builtin.Type.StructField = undefined; + for (&fields, tags) |*field, t| field.* = .{ + .name = t.name, + .type = *if (t.value < Tag.no_payload_count) void else @field(Tag, t.name).Type(), + .default_value = null, + .is_comptime = false, + .alignment = 0, + }; + break :map @Type(.{ .Struct = .{ + .layout = .Extern, + .fields = &fields, + .decls = &.{}, + .is_tuple = false, + } }); + }) void { + _ = self; + _ = tag_to_payload_map; + } + + comptime { + if (builtin.mode == .Debug) { + _ = dbHelper; + } + } }; diff --git a/src/value.zig b/src/value.zig index 5646a837ad..4a5683df36 100644 --- a/src/value.zig +++ b/src/value.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const Type = @import("type.zig").Type; const log2 = std.math.log2; const assert = std.debug.assert; @@ -5584,6 +5585,35 @@ pub const Value = extern union { ri.* = @intToEnum(RuntimeIndex, @enumToInt(ri.*) + 1); } }; + + /// This function is used in the debugger pretty formatters in tools/ to fetch the + /// Tag to Payload mapping to facilitate fancy debug printing for this type. + fn dbHelper(self: *Value, tag_to_payload_map: *map: { + const tags = @typeInfo(Tag).Enum.fields; + var fields: [tags.len]std.builtin.Type.StructField = undefined; + for (&fields, tags) |*field, t| field.* = .{ + .name = t.name, + .type = *if (t.value < Tag.no_payload_count) void else @field(Tag, t.name).Type(), + .default_value = null, + .is_comptime = false, + .alignment = 0, + }; + break :map @Type(.{ .Struct = .{ + .layout = .Extern, + .fields = &fields, + .decls = &.{}, + .is_tuple = false, + } }); + }) void { + _ = self; + _ = tag_to_payload_map; + } + + comptime { + if (builtin.mode == .Debug) { + _ = dbHelper; + } + } }; var negative_one_payload: Value.Payload.I64 = .{ diff --git a/tools/lldb_pretty_printers.py b/tools/lldb_pretty_printers.py new file mode 100644 index 0000000000..3013fbb43e --- /dev/null +++ b/tools/lldb_pretty_printers.py @@ -0,0 +1,575 @@ +# pretty printing for the zig language, zig standard library, and zig stage 2 compiler. +# put commands in ~/.lldbinit to run them automatically when starting lldb +# `command script /path/to/stage2_lldb_pretty_printers.py` to import this file +# `type category enable zig` to enable pretty printing for the zig language +# `type category enable zig.std` to enable pretty printing for the zig standard library +# `type category enable zig.stage2` to enable pretty printing for the zig stage 2 compiler +import lldb +import re + +page_size = 1 << 12 + +def log2_int(i): return i.bit_length() - 1 + +# Define Zig Language + +zig_keywords = { + 'addrspace', + 'align', + 'allowzero', + 'and', + 'anyframe', + 'anytype', + 'asm', + 'async', + 'await', + 'break', + 'callconv', + 'catch', + 'comptime', + 'const', + 'continue', + 'defer', + 'else', + 'enum', + 'errdefer', + 'error', + 'export', + 'extern', + 'fn', + 'for', + 'if', + 'inline', + 'noalias', + 'noinline', + 'nosuspend', + 'opaque', + 'or', + 'orelse', + 'packed', + 'pub', + 'resume', + 'return', + 'linksection', + 'struct', + 'suspend', + 'switch', + 'test', + 'threadlocal', + 'try', + 'union', + 'unreachable', + 'usingnamespace', + 'var', + 'volatile', + 'while', +} +zig_primitives = { + 'anyerror', + 'anyframe', + 'anyopaque', + 'bool', + 'c_int', + 'c_long', + 'c_longdouble', + 'c_longlong', + 'c_short', + 'c_uint', + 'c_ulong', + 'c_ulonglong', + 'c_ushort', + 'comptime_float', + 'comptime_int', + 'f128', + 'f16', + 'f32', + 'f64', + 'f80', + 'false', + 'isize', + 'noreturn', + 'null', + 'true', + 'type', + 'undefined', + 'usize', + 'void', +} +zig_integer_type = re.compile('[iu][1-9][0-9]+') +zig_identifier_regex = re.compile('[A-Z_a-z][0-9A-Z_a-z]*') +def zig_IsVariableName(string): return string != '_' and string not in zig_keywords and string not in zig_primitives and not zig_integer_type.fullmatch(string) and zig_identifier_regex.fullmatch(string) +def zig_IsFieldName(string): return string not in zig_keywords and zig_identifier_regex.fullmatch(string) + +class zig_Slice_SynthProvider: + def __init__(self, value, _=None): self.value = value + def update(self): + try: + self.ptr = self.value.GetChildMemberWithName('ptr') + self.len = self.value.GetChildMemberWithName('len').unsigned if self.ptr.unsigned > page_size else 0 + self.elem_type = self.ptr.type.GetPointeeType() + self.elem_size = self.elem_type.size + except: pass + def has_children(self): return True + def num_children(self): return self.len or 0 + def get_child_index(self, name): + try: return int(name.removeprefix('[').removesuffix(']')) + except: return -1 + def get_child_at_index(self, index): + if index < 0 or index >= self.len: return None + try: return self.ptr.CreateChildAtOffset('[%d]' % index, index * self.elem_size, self.elem_type) + except: return None + +def zig_String_decode(value, offset=0, length=None): + try: + value = value.GetNonSyntheticValue() + data = value.GetChildMemberWithName('ptr').GetPointeeData(offset, length if length is not None else value.GetChildMemberWithName('len').unsigned) + b = bytes(data.uint8) + b = b.replace(b'\\', b'\\\\') + b = b.replace(b'\n', b'\\n') + b = b.replace(b'\r', b'\\r') + b = b.replace(b'\t', b'\\t') + b = b.replace(b'"', b'\\"') + b = b.replace(b'\'', b'\\\'') + s = b.decode(encoding='ascii', errors='backslashreplace') + return s if s.isprintable() else ''.join((c if c.isprintable() else '\\x%02x' % ord(c) for c in s)) + except: return None +def zig_String_SummaryProvider(value, _=None): return '"%s"' % zig_String_decode(value) +def zig_String_AsIdentifier(value, pred): + string = zig_String_decode(value) + return string if pred(string) else '@"%s"' % string + +class zig_Optional_SynthProvider: + def __init__(self, value, _=None): self.value = value + def update(self): + try: + self.child = self.value.GetChildMemberWithName('some').unsigned == 1 and self.value.GetChildMemberWithName('data').Clone('child') + except: pass + def has_children(self): return bool(self.child) + def num_children(self): return int(self.child) + def get_child_index(self, name): return 0 if self.child and (name == 'child' or name == '?') else -1 + def get_child_at_index(self, index): return self.child if self.child and index == 0 else None +def zig_Optional_SummaryProvider(value, _=None): + child = value.GetChildMemberWithName('child') + return child or 'null' + +class zig_ErrorUnion_SynthProvider: + def __init__(self, value, _=None): self.value = value + def update(self): + try: + self.error_set = self.value.GetChildMemberWithName('tag').Clone('error_set') + self.payload = self.value.GetChildMemberWithName('value').Clone('payload') if self.error_set.unsigned == 0 else None + except: pass + def has_children(self): return True + def num_children(self): return 1 + def get_child_index(self, name): return 0 if name == ('payload' if self.payload else 'error_set') else -1 + def get_child_at_index(self, index): return self.payload or self.error_set if index == 0 else None + +# Define Zig Standard Library + +class std_SegmentedList_SynthProvider: + def __init__(self, value, _=None): self.value = value + def update(self): + try: + self.prealloc_segment = self.value.GetChildMemberWithName('prealloc_segment') + self.dynamic_segments = zig_Slice_SynthProvider(self.value.GetChildMemberWithName('dynamic_segments')) + self.dynamic_segments.update() + self.len = self.value.GetChildMemberWithName('len').unsigned + except: pass + def has_children(self): return True + def num_children(self): return self.len + def get_child_index(self, name): + try: return int(name.removeprefix('[').removesuffix(']')) + except: return -1 + def get_child_at_index(self, index): + try: + if index < 0 or index >= self.len: return None + prealloc_item_count = len(self.prealloc_segment) + if index < prealloc_item_count: return self.prealloc_segment.child[index] + prealloc_exp = prealloc_item_count.bit_length() - 1 + shelf_index = log2_int(index + 1) if prealloc_item_count == 0 else log2_int(index + prealloc_item_count) - prealloc_exp - 1 + shelf = self.dynamic_segments.get_child_at_index(shelf_index) + box_index = (index + 1) - (1 << shelf_index) if prealloc_item_count == 0 else index + prealloc_item_count - (1 << ((prealloc_exp + 1) + shelf_index)) + elem_type = shelf.type.GetPointeeType() + return shelf.CreateChildAtOffset('[%d]' % index, box_index * elem_type.size, elem_type) + except: return None + +class std_MultiArrayList_SynthProvider: + def __init__(self, value, _=None): self.value = value + def update(self): + try: + self.len = 0 + + value_type = self.value.type + for helper in self.value.target.FindFunctions('%s.dbHelper' % value_type.name, lldb.eFunctionNameTypeFull): + ptr_self_type, ptr_child_type, ptr_entry_type = helper.function.type.GetFunctionArgumentTypes() + if ptr_self_type.GetPointeeType() == value_type: break + else: return + + self.entry_type = ptr_entry_type.GetPointeeType() + self.bytes = self.value.GetChildMemberWithName('bytes') + self.len = self.value.GetChildMemberWithName('len').unsigned + self.capacity = self.value.GetChildMemberWithName('capacity').unsigned + except: pass + def has_children(self): return True + def num_children(self): return self.len + def get_child_index(self, name): + try: return int(name.removeprefix('[').removesuffix(']')) + except: return -1 + def get_child_at_index(self, index): + try: + if index < 0 or index >= self.len: return None + offset = 0 + data = lldb.SBData() + for field in self.entry_type.fields: + ptr_field_type = field.type + field_size = ptr_field_type.GetPointeeType().size + data.Append(self.bytes.CreateChildAtOffset(field.name, offset + index * field_size, ptr_field_type).address_of.data) + offset += self.capacity * field_size + return self.bytes.CreateValueFromData('[%d]' % index, data, self.entry_type) + except: return None + +class std_HashMapUnmanaged_SynthProvider: + def __init__(self, value, _=None): self.value = value + def update(self): + try: + self.capacity = 0 + self.indices = tuple() + + self.metadata = self.value.GetChildMemberWithName('metadata') + if not self.metadata.unsigned: return + + value_type = self.value.type + for helper in self.value.target.FindFunctions('%s.dbHelper' % value_type.name, lldb.eFunctionNameTypeFull): + ptr_self_type, ptr_hdr_type, ptr_entry_type = helper.function.type.GetFunctionArgumentTypes() + if ptr_self_type.GetPointeeType() == value_type: break + else: return + self.entry_type = ptr_entry_type.GetPointeeType() + + hdr_type = ptr_hdr_type.GetPointeeType() + hdr = self.metadata.CreateValueFromAddress('header', self.metadata.deref.load_addr - hdr_type.size, hdr_type) + self.values = hdr.GetChildMemberWithName('values') + self.keys = hdr.GetChildMemberWithName('keys') + self.capacity = hdr.GetChildMemberWithName('capacity').unsigned + + self.indices = tuple(i for i, value in enumerate(self.metadata.GetPointeeData(0, self.capacity).sint8) if value < 0) + except: pass + def has_children(self): return True + def num_children(self): return len(self.indices) + def get_capacity(self): return self.capacity + def get_child_index(self, name): + try: return int(name.removeprefix('[').removesuffix(']')) + except: return -1 + def get_child_at_index(self, index): + try: + fields = {name: base.CreateChildAtOffset(name, self.indices[index] * pointee_type.size, pointee_type).address_of.data for name, base, pointee_type in ((name, base, base.type.GetPointeeType()) for name, base in (('key_ptr', self.keys), ('value_ptr', self.values)))} + data = lldb.SBData() + for field in self.entry_type.fields: data.Append(fields[field.name]) + return self.metadata.CreateValueFromData('[%d]' % index, data, self.entry_type) + except: return None +def std_HashMapUnmanaged_SummaryProvider(value, _=None): + synth = std_HashMapUnmanaged_SynthProvider(value.GetNonSyntheticValue(), _) + synth.update() + return 'len=%d capacity=%d' % (synth.num_children(), synth.get_capacity()) + +# formats a struct of fields of the form `name_ptr: *Type` by auto dereferencing its fields +class std_Entry_SynthProvider: + def __init__(self, value, _=None): self.value = value + def update(self): + try: + self.children = tuple(child.Clone(child.name.removesuffix('_ptr')) for child in self.value.children if child.type.GetPointeeType().size != 0) + self.indices = {child.name: i for i, child in enumerate(self.children)} + except: pass + def has_children(self): return self.num_children() != 0 + def num_children(self): return len(self.children) + def get_child_index(self, name): return self.indices.get(name) + def get_child_at_index(self, index): return self.children[index].deref if index >= 0 and index < len(self.children) else None + +# Define Zig Stage2 Compiler + +class TagAndPayload_SynthProvider: + def __init__(self, value, _=None): self.value = value + def update(self): + try: + self.tag = self.value.GetChildMemberWithName('tag') or self.value.GetChildMemberWithName('tag_ptr').deref.Clone('tag') + data = self.value.GetChildMemberWithName('data_ptr') or self.value.GetChildMemberWithName('data') + self.payload = data.GetChildMemberWithName('payload').GetChildMemberWithName(data.GetChildMemberWithName('tag').value) + except: pass + def has_children(self): return True + def num_children(self): return 2 + def get_child_index(self, name): + try: return ('tag', 'payload').index(name) + except: return -1 + def get_child_at_index(self, index): return (self.tag, self.payload)[index] if index >= 0 and index < 2 else None + +def Inst_Ref_SummaryProvider(value, _=None): + members = value.type.enum_members + return value if any(value.unsigned == member.unsigned for member in members) else 'instructions[%d]' % (value.unsigned - len(members)) + +class Module_Decl__Module_Decl_Index_SynthProvider: + def __init__(self, value, _=None): self.value = value + def update(self): + try: + for frame in self.value.thread: + mod = frame.FindVariable('mod') or frame.FindVariable('module') + if mod: break + else: return + self.ptr = mod.GetChildMemberWithName('allocated_decls').GetChildAtIndex(self.value.unsigned).Clone('decl') + except: pass + def has_children(self): return True + def num_children(self): return 1 + def get_child_index(self, name): return 0 if name == 'decl' else -1 + def get_child_at_index(self, index): return self.ptr if index == 0 else None + +class TagOrPayloadPtr_SynthProvider: + def __init__(self, value, _=None): self.value = value + def update(self): + try: + value_type = self.value.type + for helper in self.value.target.FindFunctions('%s.dbHelper' % value_type.name, lldb.eFunctionNameTypeFull): + ptr_self_type, ptr_tag_to_payload_map_type = helper.function.type.GetFunctionArgumentTypes() + self_type = ptr_self_type.GetPointeeType() + if self_type == value_type: break + else: return + tag_to_payload_map = {field.name: field.type for field in ptr_tag_to_payload_map_type.GetPointeeType().fields} + + tag = self.value.GetChildMemberWithName('tag_if_small_enough') + if tag.unsigned < page_size: + self.tag = tag.Clone('tag') + self.payload = None + else: + ptr_otherwise = self.value.GetChildMemberWithName('ptr_otherwise') + self.tag = ptr_otherwise.GetChildMemberWithName('tag') + self.payload = ptr_otherwise.Cast(tag_to_payload_map[self.tag.value]).GetChildMemberWithName('data').Clone('payload') + except: pass + def has_children(self): return True + def num_children(self): return 1 + (self.payload is not None) + def get_child_index(self, name): + try: return ('tag', 'payload').index(name) + except: return -1 + def get_child_at_index(self, index): return (self.tag, self.payload)[index] if index >= 0 and index < 2 else None + +def Module_Decl_name(decl): + error = lldb.SBError() + return decl.process.ReadCStringFromMemory(decl.GetChildMemberWithName('name').deref.load_addr, 256, error) + +def Module_Namespace_RenderFullyQualifiedName(namespace): + parent = namespace.GetChildMemberWithName('parent') + if parent.unsigned < page_size: return zig_String_decode(namespace.GetChildMemberWithName('file_scope').GetChildMemberWithName('sub_file_path')).removesuffix('.zig').replace('/', '.') + return '.'.join((Module_Namespace_RenderFullyQualifiedName(parent), Module_Decl_name(namespace.GetChildMemberWithName('ty').GetChildMemberWithName('payload').GetChildMemberWithName('owner_decl').GetChildMemberWithName('decl')))) + +def Module_Decl_RenderFullyQualifiedName(decl): return '.'.join((Module_Namespace_RenderFullyQualifiedName(decl.GetChildMemberWithName('src_namespace')), Module_Decl_name(decl))) + +def OwnerDecl_RenderFullyQualifiedName(payload): return Module_Decl_RenderFullyQualifiedName(payload.GetChildMemberWithName('owner_decl').GetChildMemberWithName('decl')) + +def type_Type_pointer(payload): + pointee_type = payload.GetChildMemberWithName('pointee_type') + sentinel = payload.GetChildMemberWithName('sentinel').GetChildMemberWithName('child') + align = payload.GetChildMemberWithName('align').unsigned + addrspace = payload.GetChildMemberWithName('addrspace').value + bit_offset = payload.GetChildMemberWithName('bit_offset').unsigned + host_size = payload.GetChildMemberWithName('host_size').unsigned + vector_index = payload.GetChildMemberWithName('vector_index') + allowzero = payload.GetChildMemberWithName('allowzero').unsigned + const = not payload.GetChildMemberWithName('mutable').unsigned + volatile = payload.GetChildMemberWithName('volatile').unsigned + size = payload.GetChildMemberWithName('size').value + + if size == 'One': summary = '*' + elif size == 'Many': summary = '[*' + elif size == 'Slice': summary = '[' + elif size == 'C': summary = '[*c' + if sentinel: summary += ':%s' % value_Value_SummaryProvider(sentinel) + if size != 'One': summary += ']' + if allowzero: summary += 'allowzero ' + if align != 0 or host_size != 0 or vector_index.value != 'none': summary += 'align(%d%s%s) ' % (align, ':%d:%d' % (bit_offset, host_size) if bit_offset != 0 or host_size != 0 else '', ':?' if vector_index.value == 'runtime' else ':%d' % vector_index.unsigned if vector_index.value != 'none' else '') + if addrspace != 'generic': summary += 'addrspace(.%s) ' % addrspace + if const: summary += 'const ' + if volatile: summary += 'volatile ' + summary += type_Type_SummaryProvider(pointee_type) + return summary + +def type_Type_function(payload): + param_types = payload.GetChildMemberWithName('param_types').children + comptime_params = payload.GetChildMemberWithName('comptime_params').GetPointeeData(0, len(param_types)).uint8 + return_type = payload.GetChildMemberWithName('return_type') + alignment = payload.GetChildMemberWithName('alignment').unsigned + noalias_bits = payload.GetChildMemberWithName('noalias_bits').unsigned + cc = payload.GetChildMemberWithName('cc').value + is_var_args = payload.GetChildMemberWithName('is_var_args').unsigned + + return 'fn(%s)%s%s %s' % (', '.join(tuple(''.join(('comptime ' if comptime_param else '', 'noalias ' if noalias_bits & 1 << i else '', type_Type_SummaryProvider(param_type))) for i, (comptime_param, param_type) in enumerate(zip(comptime_params, param_types))) + (('...',) if is_var_args else ())), ' align(%d)' % alignment if alignment != 0 else '', ' callconv(.%s)' % cc if cc != 'Unspecified' else '', type_Type_SummaryProvider(return_type)) + +def type_Type_SummaryProvider(value, _=None): + tag = value.GetChildMemberWithName('tag').value + return type_tag_handlers.get(tag, lambda payload: tag)(value.GetChildMemberWithName('payload')) + +type_tag_handlers = { + 'atomic_order': lambda payload: 'std.builtin.AtomicOrder', + 'atomic_rmw_op': lambda payload: 'std.builtin.AtomicRmwOp', + 'calling_convention': lambda payload: 'std.builtin.CallingConvention', + 'address_space': lambda payload: 'std.builtin.AddressSpace', + 'float_mode': lambda payload: 'std.builtin.FloatMode', + 'reduce_op': lambda payload: 'std.builtin.ReduceOp', + 'modifier': lambda payload: 'std.builtin.CallModifier', + 'prefetch_options': lambda payload: 'std.builtin.PrefetchOptions', + 'export_options': lambda payload: 'std.builtin.ExportOptions', + 'extern_options': lambda payload: 'std.builtin.ExternOptions', + 'type_info': lambda payload: 'std.builtin.Type', + + 'enum_literal': lambda payload: '@TypeOf(.enum_literal)', + 'null': lambda payload: '@TypeOf(null)', + 'undefined': lambda payload: '@TypeOf(undefined)', + 'empty_struct_literal': lambda payload: '@TypeOf(.{})', + + 'anyerror_void_error_union': lambda payload: 'anyerror!void', + 'const_slice_u8': lambda payload: '[]const u8', + 'const_slice_u8_sentinel_0': lambda payload: '[:0]const u8', + 'fn_noreturn_no_args': lambda payload: 'fn() noreturn', + 'fn_void_no_args': lambda payload: 'fn() void', + 'fn_naked_noreturn_no_args': lambda payload: 'fn() callconv(.Naked) noreturn', + 'fn_ccc_void_no_args': lambda payload: 'fn() callconv(.C) void', + 'single_const_pointer_to_comptime_int': lambda payload: '*const comptime_int', + 'manyptr_u8': lambda payload: '[*]u8', + 'manyptr_const_u8': lambda payload: '[*]const u8', + 'manyptr_const_u8_sentinel_0': lambda payload: '[*:0]const u8', + + 'function': type_Type_function, + 'error_union': lambda payload: '%s!%s' % (type_Type_SummaryProvider(payload.GetChildMemberWithName('error_set')), type_Type_SummaryProvider(payload.GetChildMemberWithName('payload'))), + 'array_u8': lambda payload: '[%d]u8' % payload.unsigned, + 'array_u8_sentinel_0': lambda payload: '[%d:0]u8' % payload.unsigned, + 'vector': lambda payload: '@Vector(%d, %s)' % (payload.GetChildMemberWithName('len').unsigned, type_Type_SummaryProvider(payload.GetChildMemberWithName('elem_type'))), + 'array': lambda payload: '[%d]%s' % (payload.GetChildMemberWithName('len').unsigned, type_Type_SummaryProvider(payload.GetChildMemberWithName('elem_type'))), + 'array_sentinel': lambda payload: '[%d:%s]%s' % (payload.GetChildMemberWithName('len').unsigned, value_Value_SummaryProvider(payload.GetChildMemberWithName('sentinel')), type_Type_SummaryProvider(payload.GetChildMemberWithName('elem_type'))), + 'tuple': lambda payload: 'tuple{%s}' % ', '.join(('comptime %%s = %s' % value_Value_SummaryProvider(value) if value.GetChildMemberWithName('tag').value != 'unreachable_value' else '%s') % type_Type_SummaryProvider(type) for type, value in zip(payload.GetChildMemberWithName('types').children, payload.GetChildMemberWithName('values').children)), + 'anon_struct': lambda payload: 'struct{%s}' % ', '.join(('comptime %%s: %%s = %s' % value_Value_SummaryProvider(value) if value.GetChildMemberWithName('tag').value != 'unreachable_value' else '%s: %s') % (zig_String_AsIdentifier(name, zig_IsFieldName), type_Type_SummaryProvider(type)) for name, type, value in zip(payload.GetChildMemberWithName('names').children, payload.GetChildMemberWithName('types').children, payload.GetChildMemberWithName('values').children)), + 'pointer': type_Type_pointer, + 'single_const_pointer': lambda payload: '*const %s' % type_Type_SummaryProvider(payload), + 'single_mut_pointer': lambda payload: '*%s' % type_Type_SummaryProvider(payload), + 'many_const_pointer': lambda payload: '[*]const %s' % type_Type_SummaryProvider(payload), + 'many_mut_pointer': lambda payload: '[*]%s' % type_Type_SummaryProvider(payload), + 'c_const_pointer': lambda payload: '[*c]const %s' % type_Type_SummaryProvider(payload), + 'c_mut_pointer': lambda payload: '[*c]%s' % type_Type_SummaryProvider(payload), + 'const_slice': lambda payload: '[]const %s' % type_Type_SummaryProvider(payload), + 'mut_slice': lambda payload: '[]%s' % type_Type_SummaryProvider(payload), + 'int_signed': lambda payload: 'i%d' % payload.unsigned, + 'int_unsigned': lambda payload: 'u%d' % payload.unsigned, + 'optional': lambda payload: '?%s' % type_Type_SummaryProvider(payload), + 'optional_single_mut_pointer': lambda payload: '?*%s' % type_Type_SummaryProvider(payload), + 'optional_single_const_pointer': lambda payload: '?*const %s' % type_Type_SummaryProvider(payload), + 'anyframe_T': lambda payload: 'anyframe->%s' % type_Type_SummaryProvider(payload), + 'error_set': lambda payload: type_tag_handlers['error_set_merged'](payload.GetChildMemberWithName('names')), + 'error_set_single': lambda payload: 'error{%s}' % zig_String_AsIdentifier(payload, zig_IsFieldName), + 'error_set_merged': lambda payload: 'error{%s}' % ','.join(zig_String_AsIdentifier(child.GetChildMemberWithName('key'), zig_IsFieldName) for child in payload.GetChildMemberWithName('entries').children), + 'error_set_inferred': lambda payload: '@typeInfo(@typeInfo(@TypeOf(%s)).Fn.return_type.?).ErrorUnion.error_set' % OwnerDecl_RenderFullyQualifiedName(payload.GetChildMemberWithName('func')), + + 'enum_full': OwnerDecl_RenderFullyQualifiedName, + 'enum_nonexhaustive': OwnerDecl_RenderFullyQualifiedName, + 'enum_numbered': OwnerDecl_RenderFullyQualifiedName, + 'enum_simple': OwnerDecl_RenderFullyQualifiedName, + 'struct': OwnerDecl_RenderFullyQualifiedName, + 'union': OwnerDecl_RenderFullyQualifiedName, + 'union_safety_tagged': OwnerDecl_RenderFullyQualifiedName, + 'union_tagged': OwnerDecl_RenderFullyQualifiedName, + 'opaque': OwnerDecl_RenderFullyQualifiedName, +} + +def value_Value_str_lit(payload): + for frame in payload.thread: + mod = frame.FindVariable('mod') or frame.FindVariable('module') + if mod: break + else: return + return '"%s"' % zig_String_decode(mod.GetChildMemberWithName('string_literal_bytes').GetChildMemberWithName('items'), payload.GetChildMemberWithName('index').unsigned, payload.GetChildMemberWithName('len').unsigned) + +def value_Value_SummaryProvider(value, _=None): + tag = value.GetChildMemberWithName('tag').value + return value_tag_handlers.get(tag, lambda payload: tag.removesuffix('_type'))(value.GetChildMemberWithName('payload')) + +value_tag_handlers = { + 'undef': lambda payload: 'undefined', + 'zero': lambda payload: '0', + 'one': lambda payload: '1', + 'void_value': lambda payload: '{}', + 'unreachable_value': lambda payload: 'unreachable', + 'null_value': lambda payload: 'null', + 'bool_true': lambda payload: 'true', + 'bool_false': lambda payload: 'false', + + 'empty_struct_value': lambda payload: '.{}', + 'empty_array': lambda payload: '.{}', + + 'ty': type_Type_SummaryProvider, + 'int_type': lambda payload: '%c%d' % (payload.GetChildMemberWithName('bits').unsigned, 's' if payload.GetChildMemberWithName('signed').unsigned == 1 else 'u'), + 'int_u64': lambda payload: '%d' % payload.unsigned, + 'int_i64': lambda payload: '%d' % payload.signed, + 'int_big_positive': lambda payload: sum(child.unsigned << i * child.type.size * 8 for i, child in enumerate(payload.children)), + 'int_big_negative': lambda payload: '-%s' % value_tag_handlers['int_big_positive'](payload), + 'function': OwnerDecl_RenderFullyQualifiedName, + 'extern_fn': OwnerDecl_RenderFullyQualifiedName, + 'variable': lambda payload: value_Value_SummaryProvider(payload.GetChildMemberWithName('decl').GetChildMemberWithName('val')), + 'runtime_value': value_Value_SummaryProvider, + 'decl_ref': lambda payload: value_Value_SummaryProvider(payload.GetChildMemberWithName('decl').GetChildMemberWithName('val')), + 'decl_ref_mut': lambda payload: value_Value_SummaryProvider(payload.GetChildMemberWithName('decl_index').GetChildMemberWithName('decl').GetChildMemberWithName('val')), + 'comptime_field_ptr': lambda payload: '&%s' % value_Value_SummaryProvider(payload.GetChildMemberWithName('field_val')), + 'elem_ptr': lambda payload: '(%s)[%d]' % (value_Value_SummaryProvider(payload.GetChildMemberWithName('array_ptr')), payload.GetChildMemberWithName('index').unsigned), + 'field_ptr': lambda payload: '(%s).field[%d]' % (value_Value_SummaryProvider(payload.GetChildMemberWithName('container_ptr')), payload.GetChildMemberWithName('field_index').unsigned), + 'bytes': lambda payload: '"%s"' % zig_String_decode(payload), + 'str_lit': value_Value_str_lit, + 'repeated': lambda payload: '.{%s} ** _' % value_Value_SummaryProvider(payload), + 'empty_array_sentinel': lambda payload: '.{%s}' % value_Value_SummaryProvider(payload), + 'slice': lambda payload: '(%s)[0..%s]' % tuple(value_Value_SummaryProvider(payload.GetChildMemberWithName(name)) for name in ('ptr', 'len')), + 'float_16': lambda payload: payload.value, + 'float_32': lambda payload: payload.value, + 'float_64': lambda payload: payload.value, + 'float_80': lambda payload: payload.value, + 'float_128': lambda payload: payload.value, + 'enum_literal': lambda payload: '.%s' % zig_String_AsIdentifier(payload, zig_IsFieldName), + 'enum_field_index': lambda payload: 'field[%d]' % payload.unsigned, + 'error': lambda payload: 'error.%s' % zig_String_AsIdentifier(payload.GetChildMemberWithName('name'), zig_IsFieldName), + 'eu_payload': value_Value_SummaryProvider, + 'eu_payload_ptr': lambda payload: '&((%s).* catch unreachable)' % value_Value_SummaryProvider(payload.GetChildMemberWithName('container_ptr')), + 'opt_payload': value_Value_SummaryProvider, + 'opt_payload_ptr': lambda payload: '&(%s).*.?' % value_Value_SummaryProvider(payload.GetChildMemberWithName('container_ptr')), + 'aggregate': lambda payload: '.{%s}' % ', '.join(map(value_Value_SummaryProvider, payload.children)), + 'union': lambda payload: '.{.%s = %s}' % tuple(value_Value_SummaryProvider(payload.GetChildMemberWithName(name)) for name in ('tag', 'val')), + + 'lazy_align': lambda payload: '@alignOf(%s)' % type_Type_SummaryProvider(payload), + 'lazy_size': lambda payload: '@sizeOf(%s)' % type_Type_SummaryProvider(payload), +} + +# Initialize + +def add(debugger, *, category, regex=False, type, identifier=None, synth=False, inline_children=False, expand=False, summary=False): + prefix = '.'.join((__name__, (identifier or type).replace('.', '_').replace(':', '_'))) + if summary: debugger.HandleCommand('type summary add --category %s%s%s "%s"' % (category, ' --inline-children' if inline_children else ''.join((' --expand' if expand else '', ' --python-function %s_SummaryProvider' % prefix if summary == True else ' --summary-string "%s"' % summary)), ' --regex' if regex else '', type)) + if synth: debugger.HandleCommand('type synthetic add --category %s%s --python-class %s_SynthProvider "%s"' % (category, ' --regex' if regex else '', prefix, type)) + +def MultiArrayList_Entry(type): return '^multi_array_list\\.MultiArrayList\\(%s\\)\\.Entry__struct_[1-9][0-9]*$' % type + +def __lldb_init_module(debugger, _=None): + # Initialize Zig Language + add(debugger, category='zig', regex=True, type='^\\[\\]', identifier='zig_Slice', synth=True, expand=True, summary='len=${svar%#}') + add(debugger, category='zig', type='[]u8', identifier='zig_String', summary=True) + add(debugger, category='zig', regex=True, type='^\\?', identifier='zig_Optional', synth=True, summary=True) + add(debugger, category='zig', regex=True, type='^(error{.*}|anyerror)!', identifier='zig_ErrorUnion', synth=True, inline_children=True, summary=True) + + # Initialize Zig Standard Library + add(debugger, category='zig.std', type='mem.Allocator', summary='${var.ptr}') + add(debugger, category='zig.std', regex=True, type='^segmented_list\\.SegmentedList\\(.*\\)$', identifier='std_SegmentedList', synth=True, expand=True, summary='len=${var.len}') + add(debugger, category='zig.std', regex=True, type='^multi_array_list\\.MultiArrayList\\(.*\\)$', identifier='std_MultiArrayList', synth=True, expand=True, summary='len=${var.len} capacity=${var.capacity}') + add(debugger, category='zig.std', regex=True, type=MultiArrayList_Entry('.*'), identifier='std_Entry', synth=True, inline_children=True, summary=True) + add(debugger, category='zig.std', regex=True, type='^hash_map\\.HashMapUnmanaged\\(.*\\)$', identifier='std_HashMapUnmanaged', synth=True, expand=True, summary=True) + add(debugger, category='zig.std', regex=True, type='^hash_map\\.HashMapUnmanaged\\(.*\\)\\.Entry$', identifier = 'std_Entry', synth=True, inline_children=True, summary=True) + + # Initialize Zig Stage2 Compiler + add(debugger, category='zig.stage2', type='Zir.Inst', identifier='TagAndPayload', synth=True, inline_children=True, summary=True) + add(debugger, category='zig.stage2', regex=True, type=MultiArrayList_Entry('Zir\\.Inst'), identifier='TagAndPayload', synth=True, inline_children=True, summary=True) + add(debugger, category='zig.stage2', regex=True, type='^Zir\\.Inst\\.Data\\.Data__struct_[1-9][0-9]*$', inline_children=True, summary=True) + add(debugger, category='zig.stage2', type='Zir.Inst::Zir.Inst.Ref', identifier='Inst_Ref', summary=True) + add(debugger, category='zig.stage2', type='Air.Inst', identifier='TagAndPayload', synth=True, inline_children=True, summary=True) + add(debugger, category='zig.stage2', regex=True, type=MultiArrayList_Entry('Air\\.Inst'), identifier='TagAndPayload', synth=True, inline_children=True, summary=True) + add(debugger, category='zig.stage2', regex=True, type='^Air\\.Inst\\.Data\\.Data__struct_[1-9][0-9]*$', inline_children=True, summary=True) + add(debugger, category='zig.stage2', type='Module.Decl::Module.Decl.Index', synth=True) + add(debugger, category='zig.stage2', type='type.Type', identifier='TagOrPayloadPtr', synth=True) + add(debugger, category='zig.stage2', type='type.Type', summary=True) + add(debugger, category='zig.stage2', type='value.Value', identifier='TagOrPayloadPtr', synth=True) + add(debugger, category='zig.stage2', type='value.Value', summary=True) diff --git a/tools/stage2_lldb_pretty_printers.py b/tools/stage2_lldb_pretty_printers.py deleted file mode 100644 index 3dcb06f538..0000000000 --- a/tools/stage2_lldb_pretty_printers.py +++ /dev/null @@ -1,59 +0,0 @@ -# pretty printing for stage 2. -# put "command script /path/to/stage2_lldb_pretty_printers.py" and "type category enable stage2" in ~/.lldbinit to load it automatically. -import lldb -import stage2_pretty_printers_common as common - -category = 'stage2' -module = category + '_lldb_pretty_printers' - -class type_Type_SynthProvider: - def __init__(self, type, _=None): - self.type = type - - def update(self): - self.tag = self.type.GetChildMemberWithName('tag_if_small_enough').Clone('tag') - self.payload = None - if self.tag.GetValueAsUnsigned() >= common.Type.no_payload_count: - ptr_otherwise = self.type.GetChildMemberWithName('ptr_otherwise') - self.tag = ptr_otherwise.Dereference().GetChildMemberWithName('tag') - payload_type = self.type.target.FindFirstType('type.' + common.Type.payload_type_names[self.tag.GetValue()]) - self.payload = ptr_otherwise.Cast(payload_type.GetPointerType()).Dereference().GetChildMemberWithName('data').Clone('payload') - - def num_children(self): - return 1 + (self.payload is not None) - - def get_child_index(self, name): - return ['tag', 'payload'].index(name) - - def get_child_at_index(self, index): - return [self.tag, self.payload][index] - -class value_Value_SynthProvider: - def __init__(self, value, _=None): - self.value = value - - def update(self): - self.tag = self.value.GetChildMemberWithName('tag_if_small_enough').Clone('tag') - self.payload = None - if self.tag.GetValueAsUnsigned() >= common.Value.no_payload_count: - ptr_otherwise = self.value.GetChildMemberWithName('ptr_otherwise') - self.tag = ptr_otherwise.Dereference().GetChildMemberWithName('tag') - payload_type = self.value.target.FindFirstType('value.' + common.Value.payload_type_names[self.tag.GetValue()]) - self.payload = ptr_otherwise.Cast(payload_type.GetPointerType()).Dereference().GetChildMemberWithName('data').Clone('payload') - - def num_children(self): - return 1 + (self.payload is not None) - - def get_child_index(self, name): - return ['tag', 'payload'].index(name) - - def get_child_at_index(self, index): - return [self.tag, self.payload][index] - -def add(debugger, type, summary=False, synth=False): - if summary: debugger.HandleCommand('type summary add --python-function ' + module + '.' + type.replace('.', '_') + '_SummaryProvider "' + type + '" --category ' + category) - if synth: debugger.HandleCommand('type synthetic add --python-class ' + module + '.' + type.replace('.', '_') + '_SynthProvider "' + type + '" --category ' + category) - -def __lldb_init_module(debugger, _=None): - add(debugger, 'type.Type', synth=True) - add(debugger, 'value.Value', synth=True) diff --git a/tools/std_gdb_pretty_printers.py b/tools/std_gdb_pretty_printers.py index c89de56fa6..a564de7c18 100644 --- a/tools/std_gdb_pretty_printers.py +++ b/tools/std_gdb_pretty_printers.py @@ -26,7 +26,7 @@ class MultiArrayListPrinter: self.val = val def child_type(self): - (helper_fn, _) = gdb.lookup_symbol('%s.gdbHelper' % self.val.type.name) + (helper_fn, _) = gdb.lookup_symbol('%s.dbHelper' % self.val.type.name) return helper_fn.type.fields()[1].type.target() def to_string(self): @@ -65,7 +65,7 @@ class HashMapPrinter: self.val = val['unmanaged'] if is_managed else val def header_ptr_type(self): - (helper_fn, _) = gdb.lookup_symbol('%s.gdbHelper' % self.val.type.name) + (helper_fn, _) = gdb.lookup_symbol('%s.dbHelper' % self.val.type.name) return helper_fn.type.fields()[1].type def header(self):