Skip to content

Commit

Permalink
lldb: add pretty printer for cau and nav indices
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobly0 committed Dec 21, 2024
1 parent 5c76e08 commit 5776d8f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 49 deletions.
126 changes: 85 additions & 41 deletions src/InternPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ pub const Cau = struct {
_ => @enumFromInt(@intFromEnum(opt)),
};
}

const debug_state = InternPool.debug_state;
};
pub fn toOptional(i: Cau.Index) Optional {
return @enumFromInt(@intFromEnum(i));
Expand All @@ -491,6 +493,8 @@ pub const Cau = struct {
.index = @intFromEnum(cau_index) & ip.getIndexMask(u31),
};
}

const debug_state = InternPool.debug_state;
};
};

Expand Down Expand Up @@ -568,6 +572,8 @@ pub const Nav = struct {
_ => @enumFromInt(@intFromEnum(opt)),
};
}

const debug_state = InternPool.debug_state;
};
pub fn toOptional(i: Nav.Index) Optional {
return @enumFromInt(@intFromEnum(i));
Expand All @@ -589,6 +595,8 @@ pub const Nav = struct {
.index = @intFromEnum(nav_index) & ip.getIndexMask(u32),
};
}

const debug_state = InternPool.debug_state;
};

/// The compact in-memory representation of a `Nav`.
Expand Down Expand Up @@ -5271,17 +5279,26 @@ pub const Tag = enum(u8) {
.type_int_signed = .{ .summary = .@"i{.data%value}", .data = u32 },
.type_int_unsigned = .{ .summary = .@"u{.data%value}", .data = u32 },
.type_array_big = .{ .summary = .@"[{.payload.len1%value} << 32 | {.payload.len0%value}:{.payload.sentinel%summary}]{.payload.child%summary}", .payload = Array },
.type_array_big = .{
.summary = .@"[{.payload.len1%value} << 32 | {.payload.len0%value}:{.payload.sentinel%summary}]{.payload.child%summary}",
.payload = Array,
},
.type_array_small = .{ .summary = .@"[{.payload.len%value}]{.payload.child%summary}", .payload = Vector },
.type_vector = .{ .summary = .@"@Vector({.payload.len%value}, {.payload.child%summary})", .payload = Vector },
.type_pointer = .{ .summary = .@"*... {.payload.child%summary}", .payload = TypePointer },
.type_slice = .{ .summary = .@"[]... {.data.unwrapped.payload.child%summary}", .data = Index },
.type_optional = .{ .summary = .@"?{.data%summary}", .data = Index },
.type_anyframe = .{ .summary = .@"anyframe->{.data%summary}", .data = Index },
.type_error_union = .{ .summary = .@"{.payload.error_set_type%summary}!{.payload.payload_type%summary}", .payload = ErrorUnionType },
.type_error_union = .{
.summary = .@"{.payload.error_set_type%summary}!{.payload.payload_type%summary}",
.payload = ErrorUnionType,
},
.type_anyerror_union = .{ .summary = .@"anyerror!{.data%summary}", .data = Index },
.type_error_set = .{ .summary = .@"error{...}", .payload = ErrorSet },
.type_inferred_error_set = .{ .summary = .@"@typeInfo(@typeInfo(@TypeOf({.data%summary})).@\"fn\".return_type.?).error_union.error_set", .data = Index },
.type_inferred_error_set = .{
.summary = .@"@typeInfo(@typeInfo(@TypeOf({.data%summary})).@\"fn\".return_type.?).error_union.error_set",
.data = Index,
},
.type_enum_auto = .{
.summary = .@"{.payload.name%summary#\"}",
.payload = EnumAuto,
Expand All @@ -5307,12 +5324,8 @@ pub const Tag = enum(u8) {
.type_opaque = .{
.summary = .@"{.payload.name%summary#\"}",
.payload = TypeOpaque,
.trailing = struct {
captures: []CaptureValue,
},
.config = .{
.@"trailing.captures.len" = .@"payload.captures_len",
},
.trailing = struct { captures: []CaptureValue },
.config = .{ .@"trailing.captures.len" = .@"payload.captures_len" },
},
.type_struct = .{
.summary = .@"{.payload.name%summary#\"}",
Expand Down Expand Up @@ -5438,17 +5451,50 @@ pub const Tag = enum(u8) {

.undef = .{ .summary = .@"@as({.data%summary}, undefined)", .data = Index },
.simple_value = .{ .summary = .@"{.index%value#.}", .index = SimpleValue },
.ptr_nav = .{ .payload = PtrNav },
.ptr_comptime_alloc = .{ .payload = PtrComptimeAlloc },
.ptr_uav = .{ .payload = PtrUav },
.ptr_uav_aligned = .{ .payload = PtrUavAligned },
.ptr_comptime_field = .{ .payload = PtrComptimeField },
.ptr_int = .{ .payload = PtrInt },
.ptr_eu_payload = .{ .payload = PtrBase },
.ptr_opt_payload = .{ .payload = PtrBase },
.ptr_elem = .{ .payload = PtrBaseIndex },
.ptr_field = .{ .payload = PtrBaseIndex },
.ptr_slice = .{ .payload = PtrSlice },
.ptr_nav = .{
.summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.nav.fqn%summary#\"}) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
.payload = PtrNav,
},
.ptr_comptime_alloc = .{
.summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&comptime_allocs[{.payload.index%summary}]) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
.payload = PtrComptimeAlloc,
},
.ptr_uav = .{
.summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.val%summary}) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
.payload = PtrUav,
},
.ptr_uav_aligned = .{
.summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(@as({.payload.orig_ty%summary}, &{.payload.val%summary})) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
.payload = PtrUavAligned,
},
.ptr_comptime_field = .{
.summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.field_val%summary}) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
.payload = PtrComptimeField,
},
.ptr_int = .{
.summary = .@"@as({.payload.ty%summary}, @ptrFromInt({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value}))",
.payload = PtrInt,
},
.ptr_eu_payload = .{
.summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&({.payload.base%summary} catch unreachable)) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
.payload = PtrBase,
},
.ptr_opt_payload = .{
.summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.base%summary}.?) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
.payload = PtrBase,
},
.ptr_elem = .{
.summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.base%summary}[{.payload.index%summary}]) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
.payload = PtrBaseIndex,
},
.ptr_field = .{
.summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.base%summary}[{.payload.index%summary}]) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
.payload = PtrBaseIndex,
},
.ptr_slice = .{
.summary = .@"{.payload.ptr%summary}[0..{.payload.len%summary}]",
.payload = PtrSlice,
},
.opt_payload = .{ .summary = .@"@as({.payload.ty%summary}, {.payload.val%summary})", .payload = TypeValue },
.opt_null = .{ .summary = .@"@as({.data%summary}, null)", .data = Index },
.int_u8 = .{ .summary = .@"@as(u8, {.data%value})", .data = u8 },
Expand Down Expand Up @@ -5476,18 +5522,16 @@ pub const Tag = enum(u8) {
.float_c_longdouble_f80 = .{ .summary = .@"@as(c_longdouble, {.payload%value})", .payload = f80 },
.float_c_longdouble_f128 = .{ .summary = .@"@as(c_longdouble, {.payload%value})", .payload = f128 },
.float_comptime_float = .{ .summary = .@"{.payload%value}", .payload = f128 },
.variable = .{ .payload = Variable },
.@"extern" = .{ .payload = Extern },
.variable = .{ .summary = .@"{.payload.owner_nav.fqn%summary#\"}", .payload = Variable },
.@"extern" = .{ .summary = .@"{.payload.owner_nav.fqn%summary#\"}", .payload = Extern },
.func_decl = .{
.summary = .@"{.payload.owner_nav.fqn%summary#\"}",
.payload = FuncDecl,
.trailing = struct {
inferred_error_set: ?Index,
},
.config = .{
.@"trailing.inferred_error_set.?" = .@"payload.analysis.inferred_error_set",
},
.trailing = struct { inferred_error_set: ?Index },
.config = .{ .@"trailing.inferred_error_set.?" = .@"payload.analysis.inferred_error_set" },
},
.func_instance = .{
.summary = .@"{.payload.owner_nav.fqn%summary#\"}",
.payload = FuncInstance,
.trailing = struct {
inferred_error_set: ?Index,
Expand All @@ -5498,30 +5542,26 @@ pub const Tag = enum(u8) {
.@"trailing.param_values.len" = .@"payload.ty.payload.params_len",
},
},
.func_coerced = .{ .payload = FuncCoerced },
.func_coerced = .{
.summary = .@"@as(*const {.payload.ty%summary}, @ptrCast(&{.payload.func%summary})).*",
.payload = FuncCoerced,
},
.only_possible_value = .{ .summary = .@"@as({.data%summary}, undefined)", .data = Index },
.union_value = .{ .summary = .@"@as({.payload.ty%summary}, {})", .payload = Union },
.bytes = .{ .summary = .@"@as({.payload.ty%summary}, {.payload.bytes%summary}.*)", .payload = Bytes },
.aggregate = .{
.summary = .@"@as({.payload.ty%summary}, .{...})",
.payload = Aggregate,
.trailing = struct {
elements: []Index,
},
.config = .{
.@"trailing.elements.len" = .@"payload.ty.payload.fields_len",
},
.trailing = struct { elements: []Index },
.config = .{ .@"trailing.elements.len" = .@"payload.ty.payload.fields_len" },
},
.repeated = .{ .summary = .@"@as({.payload.ty%summary}, @splat({.payload.elem_val%summary}))", .payload = Repeated },

.memoized_call = .{
.summary = .@"@memoize({.payload.func%summary})",
.payload = MemoizedCall,
.trailing = struct {
arg_values: []Index,
},
.config = .{
.@"trailing.arg_values.len" = .@"payload.args_len",
},
.trailing = struct { arg_values: []Index },
.config = .{ .@"trailing.arg_values.len" = .@"payload.args_len" },
},
};
fn Payload(comptime tag: Tag) type {
Expand Down Expand Up @@ -6490,6 +6530,10 @@ pub fn activate(ip: *const InternPool) void {
_ = OptionalString.debug_state;
_ = NullTerminatedString.debug_state;
_ = OptionalNullTerminatedString.debug_state;
_ = Cau.Index.debug_state;
_ = Cau.Index.Optional.debug_state;
_ = Nav.Index.debug_state;
_ = Nav.Index.Optional.debug_state;
std.debug.assert(debug_state.intern_pool == null);
debug_state.intern_pool = ip;
}
Expand Down
54 changes: 46 additions & 8 deletions tools/lldb_pretty_printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ def create_struct(parent, name, struct_type, inits):
for field in struct_type.fields:
field_size = field.type.size
field_init = inits[field.name]
field_init_type = type(field_init)
if field_init_type == bool:
field_bytes = bytes([field_init])
elif field_init_type == int:
if isinstance(field_init, int):
match struct_data.byte_order:
case lldb.eByteOrderLittle:
byte_order = 'little'
case lldb.eByteOrderBig:
byte_order = 'big'
field_bytes = field_init.to_bytes(field_size, byte_order, signed=field.type.GetTypeFlags() & lldb.eTypeIsSigned != 0)
elif field_init_type == lldb.SBValue:
elif isinstance(field_init_type, lldb.SBValue):
field_bytes = field_init.data.uint8
else: return
match struct_data.byte_order:
Expand Down Expand Up @@ -731,6 +728,7 @@ def get_child_at_index(self, index):
expr_path_re = re.compile(r'\{([^}]+)%([^%#}]+)(?:#([^%#}]+))?\}')
def root_InternPool_Index_SummaryProvider(value, _=None):
unwrapped = value.GetChildMemberWithName('unwrapped')
if not unwrapped: return '' # .none
tag = unwrapped.GetChildMemberWithName('tag')
tag_value = tag.value
summary = tag.CreateValueFromType(tag.type).GetChildMemberWithName('encodings').GetChildMemberWithName(tag_value.removeprefix('.')).GetChildMemberWithName('summary')
Expand All @@ -749,8 +747,8 @@ def update(self):
if wrapped == (1 << 32) - 1: return
unwrapped_type = self.value.type.FindDirectNestedType('Unwrapped')
ip = self.value.CreateValueFromType(unwrapped_type).GetChildMemberWithName('debug_state').GetChildMemberWithName('intern_pool').GetNonSyntheticValue().GetChildMemberWithName('?')
tid_width, tid_shift_30 = ip.GetChildMemberWithName('tid_width').unsigned, ip.GetChildMemberWithName('tid_shift_30').unsigned
self.unwrapped = create_struct(self.value, '.unwrapped', unwrapped_type, { 'tid': wrapped >> tid_shift_30 & (1 << tid_width) - 1, 'index': wrapped & (1 << tid_shift_30) - 1 })
tid_shift_30 = ip.GetChildMemberWithName('tid_shift_30').unsigned
self.unwrapped = create_struct(self.value, '.unwrapped', unwrapped_type, { 'tid': wrapped >> tid_shift_30, 'index': wrapped & (1 << tid_shift_30) - 1 })
def has_children(self): return True
def num_children(self): return 0
def get_child_index(self, name):
Expand Down Expand Up @@ -870,9 +868,9 @@ def get_child_at_index(self, index):
except: pass

def root_InternPool_String_SummaryProvider(value, _=None):
wrapped = value.unsigned
ip = value.CreateValueFromType(value.type).GetChildMemberWithName('debug_state').GetChildMemberWithName('intern_pool').GetNonSyntheticValue().GetChildMemberWithName('?')
tid_shift_32 = ip.GetChildMemberWithName('tid_shift_32').unsigned
wrapped = value.unsigned
locals_value = ip.GetChildMemberWithName('locals').GetSyntheticValue()
local_value = locals_value.child[wrapped >> tid_shift_32]
if local_value is None:
Expand All @@ -882,6 +880,44 @@ def root_InternPool_String_SummaryProvider(value, _=None):
string.format = lldb.eFormatCString
return string.value

class root_InternPool_Cau_Index_SynthProvider:
def __init__(self, value, _=None): self.value = value
def update(self):
self.cau = None
wrapped = self.value.unsigned
if wrapped == (1 << 32) - 1: return
ip = self.value.CreateValueFromType(self.value.type).GetChildMemberWithName('debug_state').GetChildMemberWithName('intern_pool').GetNonSyntheticValue().GetChildMemberWithName('?')
tid_shift_31 = ip.GetChildMemberWithName('tid_shift_31').unsigned
locals_value = ip.GetChildMemberWithName('locals').GetSyntheticValue()
local_value = locals_value.child[wrapped >> tid_shift_31]
if local_value is None:
wrapped = 0
local_value = locals_value.child[0]
self.cau = local_value.GetChildMemberWithName('shared').GetChildMemberWithName('caus').GetChildMemberWithName('view').GetChildMemberWithName('0').child[wrapped & (1 << tid_shift_31) - 1]
def has_children(self): return self.cau.GetNumChildren(1) > 0
def num_children(self): return self.cau.GetNumChildren()
def get_child_index(self, name): return self.cau.GetIndexOfChildWithName(name)
def get_child_at_index(self, index): return self.cau.GetChildAtIndex(index)

class root_InternPool_Nav_Index_SynthProvider:
def __init__(self, value, _=None): self.value = value
def update(self):
self.nav = None
wrapped = self.value.unsigned
if wrapped == (1 << 32) - 1: return
ip = self.value.CreateValueFromType(self.value.type).GetChildMemberWithName('debug_state').GetChildMemberWithName('intern_pool').GetNonSyntheticValue().GetChildMemberWithName('?')
tid_shift_32 = ip.GetChildMemberWithName('tid_shift_32').unsigned
locals_value = ip.GetChildMemberWithName('locals').GetSyntheticValue()
local_value = locals_value.child[wrapped >> tid_shift_32]
if local_value is None:
wrapped = 0
local_value = locals_value.child[0]
self.nav = local_value.GetChildMemberWithName('shared').GetChildMemberWithName('navs').GetChildMemberWithName('view').child[wrapped & (1 << tid_shift_32) - 1]
def has_children(self): return self.nav.GetNumChildren(1) > 0
def num_children(self): return self.nav.GetNumChildren()
def get_child_index(self, name): return self.nav.GetIndexOfChildWithName(name)
def get_child_at_index(self, index): return self.nav.GetChildAtIndex(index)

# Initialize

def add(debugger, *, category, regex=False, type, identifier=None, synth=False, inline_children=False, expand=False, summary=False):
Expand Down Expand Up @@ -937,3 +973,5 @@ def __lldb_init_module(debugger, _=None):
add(debugger, category='zig', type='root.InternPool.Index', synth=True, summary=True)
add(debugger, category='zig', type='root.InternPool.Index.Unwrapped', synth=True)
add(debugger, category='zig', regex=True, type=r'^root\.InternPool\.(Optional)?(NullTerminated)?String$', identifier='root_InternPool_String', summary=True)
add(debugger, category='zig', regex=True, type=r'^root\.InternPool\.Cau\.Index(\.Optional)?$', identifier='root_InternPool_Cau_Index', synth=True)
add(debugger, category='zig', regex=True, type=r'^root\.InternPool\.Nav\.Index(\.Optional)?$', identifier='root_InternPool_Nav_Index', synth=True)

0 comments on commit 5776d8f

Please sign in to comment.