From 78449b6d980fc78d418f9b44d815781eda7587f7 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Wed, 26 Jul 2023 22:10:22 -0400 Subject: [PATCH] debug: skip unwind error printing on platforms that don't have_ucontext --- lib/std/debug.zig | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 05657a2fbf..ddd0f0ec95 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -242,8 +242,7 @@ pub fn dumpStackTraceFromBase(context: *const ThreadContext) void { printSourceAtAddress(debug_info, stderr, it.unwind_state.?.dwarf_context.pc, tty_config) catch return; while (it.next()) |return_address| { - if (it.getLastError()) |unwind_error| - printUnwindError(debug_info, stderr, unwind_error.address, unwind_error.err, tty_config) catch {}; + printLastUnwindError(&it, debug_info, stderr, tty_config); // On arm64 macOS, the address of the last frame is 0x0 rather than 0x1 as on x86_64 macOS, // therefore, we do a check for `return_address == 0` before subtracting 1 from it to avoid @@ -252,10 +251,7 @@ pub fn dumpStackTraceFromBase(context: *const ThreadContext) void { // same behaviour for x86-windows-msvc const address = if (return_address == 0) return_address else return_address - 1; printSourceAtAddress(debug_info, stderr, address, tty_config) catch return; - } else { - if (it.getLastError()) |unwind_error| - printUnwindError(debug_info, stderr, unwind_error.address, unwind_error.err, tty_config) catch {}; - } + } else printLastUnwindError(&it, debug_info, stderr, tty_config); } } @@ -734,8 +730,7 @@ pub fn writeCurrentStackTrace( defer it.deinit(); while (it.next()) |return_address| { - if (it.getLastError()) |unwind_error| - try printUnwindError(debug_info, out_stream, unwind_error.address, unwind_error.err, tty_config); + printLastUnwindError(&it, debug_info, out_stream, tty_config); // On arm64 macOS, the address of the last frame is 0x0 rather than 0x1 as on x86_64 macOS, // therefore, we do a check for `return_address == 0` before subtracting 1 from it to avoid @@ -744,10 +739,7 @@ pub fn writeCurrentStackTrace( // same behaviour for x86-windows-msvc const address = if (return_address == 0) return_address else return_address - 1; try printSourceAtAddress(debug_info, out_stream, address, tty_config); - } else { - if (it.getLastError()) |unwind_error| - try printUnwindError(debug_info, out_stream, unwind_error.address, unwind_error.err, tty_config); - } + } else printLastUnwindError(&it, debug_info, out_stream, tty_config); } pub noinline fn walkStackWindows(addresses: []usize, existing_context: ?*const windows.CONTEXT) usize { @@ -885,7 +877,14 @@ fn printUnknownSource(debug_info: *DebugInfo, out_stream: anytype, address: usiz ); } -pub fn printUnwindError(debug_info: *DebugInfo, out_stream: anytype, address: usize, err: UnwindError, tty_config: io.tty.Config) !void { +fn printLastUnwindError(it: *StackIterator, debug_info: *DebugInfo, out_stream: anytype, tty_config: io.tty.Config) void { + if (!have_ucontext) return; + if (it.getLastError()) |unwind_error| { + printUnwindError(debug_info, out_stream, unwind_error.address, unwind_error.err, tty_config) catch {}; + } +} + +fn printUnwindError(debug_info: *DebugInfo, out_stream: anytype, address: usize, err: UnwindError, tty_config: io.tty.Config) !void { const module_name = debug_info.getModuleNameForAddress(address) orelse "???"; try tty_config.setColor(out_stream, .dim); if (err == error.MissingDebugInfo) {