authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-10 18:43:19-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-20 22:58:15-04:00
log891fa3b8b54428ad8065de8660869cd38876e429
tree9b6fbdcfe1a87ce2b00335f51c76b1bba05129f5
parent5dfb159e15dc7c66118d47a06536d61f65522bb9

debug: fix initialization of the optional fields on StackIterator

dwarf: documentation fixups target: enable unwind tables on macho

6 files changed, 53 insertions(+), 51 deletions(-)

lib/std/debug.zig+41-39
......@@ -159,7 +159,7 @@ pub fn copyContext(source: *const ThreadContext, dest: *ThreadContext) void {
159159 relocateContext(dest);
160160}
161161
162/// Updates any internal points in the context to reflect its current location
162/// Updates any internal pointers in the context to reflect its current location
163163pub fn relocateContext(context: *ThreadContext) void {
164164 return switch (native_os) {
165165 .macos => {
......@@ -176,7 +176,7 @@ pub const have_getcontext = @hasDecl(os.system, "getcontext") and
176176});
177177
178178/// Capture the current context. The register values in the context will reflect the
179/// state after the platform `getcontext` function returned.
179/// state after the platform `getcontext` function returns.
180180///
181181/// It is valid to call this if the platform doesn't have context capturing support,
182182/// in that case false will be returned.
......@@ -229,7 +229,7 @@ pub fn dumpStackTraceFromBase(context: *const ThreadContext) void {
229229
230230 var it = StackIterator.initWithContext(null, debug_info, context) catch return;
231231 defer it.deinit();
232 printSourceAtAddress(debug_info, stderr, it.dwarf_context.pc, tty_config) catch return;
232 printSourceAtAddress(debug_info, stderr, it.unwind_state.?.dwarf_context.pc, tty_config) catch return;
233233
234234 while (it.next()) |return_address| {
235235 if (it.getLastError()) |unwind_error|
......@@ -487,11 +487,13 @@ pub const StackIterator = struct {
487487 fp: usize,
488488
489489 // When DebugInfo and a register context is available, this iterator can unwind
490 // stacks with frames that don't use a frame pointer (ie. -fomit-frame-pointer).
491 debug_info: ?*DebugInfo,
492 dwarf_context: if (have_ucontext) DW.UnwindContext else void = undefined,
493 last_error: if (have_ucontext) ?UnwindError else void = undefined,
494 last_error_address: if (have_ucontext) usize else void = undefined,
490 // stacks with frames that don't use a frame pointer (ie. -fomit-frame-pointer),
491 // using DWARF and MachO unwind info.
492 unwind_state: if (have_ucontext) ?struct {
493 debug_info: *DebugInfo,
494 dwarf_context: DW.UnwindContext,
495 last_error: ?UnwindError = null,
496 } else void = if (have_ucontext) null else {},
495497
496498 pub fn init(first_address: ?usize, fp: ?usize) StackIterator {
497499 if (native_arch == .sparc64) {
......@@ -504,32 +506,33 @@ pub const StackIterator = struct {
504506 return StackIterator{
505507 .first_address = first_address,
506508 .fp = fp orelse @frameAddress(),
507 .debug_info = null,
508509 };
509510 }
510511
511512 pub fn initWithContext(first_address: ?usize, debug_info: *DebugInfo, context: *const os.ucontext_t) !StackIterator {
512513 var iterator = init(first_address, null);
513 iterator.debug_info = debug_info;
514 iterator.dwarf_context = try DW.UnwindContext.init(debug_info.allocator, context, &isValidMemory);
515 iterator.last_error = null;
514 iterator.unwind_state = .{
515 .debug_info = debug_info,
516 .dwarf_context = try DW.UnwindContext.init(debug_info.allocator, context, &isValidMemory),
517 };
518
516519 return iterator;
517520 }
518521
519522 pub fn deinit(self: *StackIterator) void {
520 if (have_ucontext and self.debug_info != null) self.dwarf_context.deinit();
523 if (have_ucontext and self.unwind_state != null) self.unwind_state.?.dwarf_context.deinit();
521524 }
522525
523526 pub fn getLastError(self: *StackIterator) ?struct {
524 address: usize,
525527 err: UnwindError,
528 address: usize,
526529 } {
527 if (have_ucontext) {
528 if (self.last_error) |err| {
529 self.last_error = null;
530 if (!have_ucontext) return null;
531 if (self.unwind_state) |*unwind_state| {
532 if (unwind_state.last_error) |err| {
530533 return .{
531 .address = self.last_error_address,
532534 .err = err,
535 .address = unwind_state.dwarf_context.pc,
533536 };
534537 }
535538 }
......@@ -620,13 +623,14 @@ pub const StackIterator = struct {
620623 }
621624
622625 fn next_unwind(self: *StackIterator) !usize {
623 const module = try self.debug_info.?.getModuleForAddress(self.dwarf_context.pc);
626 const unwind_state = &self.unwind_state.?;
627 const module = try unwind_state.debug_info.getModuleForAddress(unwind_state.dwarf_context.pc);
624628 switch (native_os) {
625629 .macos, .ios, .watchos, .tvos => {
626630 // __unwind_info is a requirement for unwinding on Darwin. It may fall back to DWARF, but unwinding
627631 // via DWARF before attempting to use the compact unwind info will produce incorrect results.
628632 if (module.unwind_info) |unwind_info| {
629 if (macho.unwindFrame(&self.dwarf_context, unwind_info, module.base_address)) |return_address| {
633 if (macho.unwindFrame(&unwind_state.dwarf_context, unwind_info, module.base_address)) |return_address| {
630634 return return_address;
631635 } else |err| {
632636 if (err != error.RequiresDWARFUnwind) return err;
......@@ -636,23 +640,25 @@ pub const StackIterator = struct {
636640 else => {},
637641 }
638642
639 if (try module.getDwarfInfoForAddress(self.debug_info.?.allocator, self.dwarf_context.pc)) |di| {
640 return di.unwindFrame(&self.dwarf_context, module.base_address);
643 if (try module.getDwarfInfoForAddress(unwind_state.debug_info.allocator, unwind_state.dwarf_context.pc)) |di| {
644 return di.unwindFrame(&unwind_state.dwarf_context, module.base_address);
641645 } else return error.MissingDebugInfo;
642646 }
643647
644648 fn next_internal(self: *StackIterator) ?usize {
645 if (have_ucontext and self.debug_info != null) {
646 if (self.dwarf_context.pc == 0) return null;
647 if (self.next_unwind()) |return_address| {
648 return return_address;
649 } else |err| {
650 self.last_error = err;
651 self.last_error_address = self.dwarf_context.pc;
652
653 // Fall back to fp unwinding on the first failure, as the register context won't have been updated
654 self.fp = self.dwarf_context.getFp() catch 0;
655 self.debug_info = null;
649 if (have_ucontext) {
650 if (self.unwind_state) |*unwind_state| {
651 if (unwind_state.dwarf_context.pc == 0) return null;
652 if (unwind_state.last_error == null) {
653 if (self.next_unwind()) |return_address| {
654 return return_address;
655 } else |err| {
656 unwind_state.last_error = err;
657
658 // Fall back to fp-based unwinding on the first failure
659 self.fp = unwind_state.dwarf_context.getFp() catch 0;
660 }
661 }
656662 }
657663 }
658664
......@@ -862,16 +868,12 @@ pub fn printUnwindError(debug_info: *DebugInfo, out_stream: anytype, address: us
862868pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: anytype, address: usize, tty_config: io.tty.Config) !void {
863869 const module = debug_info.getModuleForAddress(address) catch |err| switch (err) {
864870 error.MissingDebugInfo, error.InvalidDebugInfo => return printUnknownSource(debug_info, out_stream, address, tty_config),
865 else => {
866 return err;
867 },
871 else => return err,
868872 };
869873
870874 const symbol_info = module.getSymbolAtAddress(debug_info.allocator, address) catch |err| switch (err) {
871875 error.MissingDebugInfo, error.InvalidDebugInfo => return printUnknownSource(debug_info, out_stream, address, tty_config),
872 else => {
873 return err;
874 },
876 else => return err,
875877 };
876878 defer symbol_info.deinit(debug_info.allocator);
877879
lib/std/dwarf.zig+3-3
......@@ -1639,7 +1639,7 @@ pub const DwarfInfo = struct {
16391639 // In order to support reading .eh_frame from the ELF file (vs using the already-mapped section),
16401640 // scanAllUnwindInfo has already mapped any pc-relative offsets such that they we be relative to zero
16411641 // instead of the actual base address of the module. When using .eh_frame_hdr, PC can be used directly
1642 // as pointers will be decoded relative to the alreayd-mapped .eh_frame.
1642 // as pointers will be decoded relative to the already-mapped .eh_frame.
16431643 var mapped_pc: usize = undefined;
16441644 if (di.eh_frame_hdr) |header| {
16451645 const eh_frame_len = if (di.section(.eh_frame)) |eh_frame| eh_frame.len else null;
......@@ -1766,8 +1766,8 @@ pub const DwarfInfo = struct {
17661766 mem.writeIntSliceNative(usize, try abi.regBytes(context.thread_context, abi.spRegNum(context.reg_context), context.reg_context), context.cfa.?);
17671767
17681768 // The call instruction will have pushed the address of the instruction that follows the call as the return address
1769 // However, this return address may be past the end of the function if the caller was `noreturn`.
1770 // TODO: Check this on non-x86_64
1769 // However, this return address may be past the end of the function if the caller was `noreturn`. By subtracting one,
1770 // then `context.pc` will always point to an instruction within the FDE for the previous function.
17711771 const return_address = context.pc;
17721772 if (context.pc > 0) context.pc -= 1;
17731773
lib/std/dwarf/abi.zig+6-3
......@@ -26,7 +26,7 @@ pub fn ipRegNum() u8 {
2626
2727pub fn fpRegNum(reg_context: RegisterContext) u8 {
2828 return switch (builtin.cpu.arch) {
29 // GCC on OS X did the opposite of ELF for these registers (only in .eh_frame), and that is now the convention for MachO
29 // GCC on OS X historicaly did the opposite of ELF for these registers (only in .eh_frame), and that is now the convention for MachO
3030 .x86 => if (reg_context.eh_frame and reg_context.is_macho) 4 else 5,
3131 .x86_64 => 6,
3232 .arm => 11,
......@@ -75,6 +75,7 @@ fn RegValueReturnType(comptime ContextPtrType: type, comptime T: type) type {
7575 });
7676}
7777
78/// Returns a pointer to a register stored in a ThreadContext, preserving the pointer attributes of the context.
7879pub fn regValueNative(
7980 comptime T: type,
8081 thread_context_ptr: anytype,
......@@ -343,9 +344,11 @@ pub fn regBytes(
343344
344345/// Returns the ABI-defined default value this register has in the unwinding table
345346/// before running any of the CIE instructions. The DWARF spec defines these values
346// to be undefined, but allows ABI authors to override that default.
347/// to be undefined, but allows ABI authors to override that default.
347348pub fn getRegDefaultValue(reg_number: u8, out: []u8) void {
348 // TODO: Implement any ABI-specific rules for the default value for registers
349
350 // Implement any ABI-specific rules here
351
349352 _ = reg_number;
350353 @memset(out, undefined);
351354}
lib/std/dwarf/expressions.zig+1-4
......@@ -14,7 +14,7 @@ pub const ExpressionContext = struct {
1414 /// This expression is from a DWARF64 section
1515 is_64: bool = false,
1616
17 /// If specified, any addresses will pass through this function before being
17 /// If specified, any addresses will pass through this function before being acccessed
1818 isValidMemory: ?*const fn (address: usize) bool = null,
1919
2020 /// The compilation unit this expression relates to, if any
......@@ -1024,9 +1024,6 @@ pub fn Builder(comptime options: ExpressionOptions) type {
10241024 try writer.writeAll(value_bytes);
10251025 }
10261026
1027 // pub fn writeImplicitPointer(writer: anytype, ) void {
1028 // }
1029
10301027 };
10311028}
10321029
lib/std/os/linux.zig+1-1
......@@ -4695,7 +4695,7 @@ else
46954695 /// processes.
46964696 RTPRIO,
46974697
4698 /// Maximum CPU time in µs that a process scheduled under a real-time
4698 /// Maximum CPU time in µs that a process scheduled under a real-time
46994699 /// scheduling policy may consume without making a blocking system
47004700 /// call before being forcibly descheduled.
47014701 RTTIME,
src/target.zig+1-1
......@@ -510,7 +510,7 @@ pub fn clangAssemblerSupportsMcpuArg(target: std.Target) bool {
510510}
511511
512512pub fn needUnwindTables(target: std.Target) bool {
513 return target.os.tag == .windows;
513 return target.os.tag == .windows or target.ofmt == .macho;
514514}
515515
516516pub fn defaultAddressSpace(