From 37a20d39846b9056dbf6aa46cb2bed3196b56b61 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Mon, 13 Apr 2026 12:17:51 +0200 Subject: [PATCH] Allow the user to override unexpected error trace This is the only bit left in the standard library where stack trace writing code is pulled to the binary even if the user doesn't want it --- lib/std/os/windows.zig | 4 ++-- lib/std/posix.zig | 14 ++------------ lib/std/std.zig | 10 ++++++++++ src/link/MachO.zig | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index 6469194029e11584925a2de6ee8490317c479874..5655c10b8e3900651b826c0ce3aa1d6f6626f0fe 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -3952,7 +3952,7 @@ inline fn MAKELANGID(p: c_ushort, s: c_ushort) LANGID { /// and you get an unexpected error. pub fn unexpectedError(err: Win32Error) UnexpectedError { @branchHint(.cold); - if (std.posix.unexpected_error_tracing) { + if (std.options.unexpected_error_tracing) { std.debug.print("error.Unexpected: GetLastError({d}): {t}\n", .{ err, err }); std.debug.dumpCurrentStackTrace(.{ .first_address = @returnAddress() }); } @@ -3962,7 +3962,7 @@ pub fn unexpectedError(err: Win32Error) UnexpectedError { /// Call this when you made a windows NtDll call /// and you get an unexpected status. pub fn unexpectedStatus(status: NTSTATUS) UnexpectedError { - if (std.posix.unexpected_error_tracing) { + if (std.options.unexpected_error_tracing) { std.debug.print("error.Unexpected NTSTATUS=0x{x} ({s})\n", .{ @intFromEnum(status), std.enums.tagName(NTSTATUS, status) orelse "", diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 87af625e44c45bcc406c47777ec337c26ad66fd6..f61293e20e916550195c86f92fb45c258a55b89f 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -686,7 +686,7 @@ pub fn munmap(memory: []align(page_size_min) const u8) void { .SUCCESS => return, .INVAL => unreachable, // Invalid parameters. .NOMEM => unreachable, // Attempted to unmap a region in the middle of an existing mapping. - else => |e| if (unexpected_error_tracing) { + else => |e| if (std.options.unexpected_error_tracing) { std.debug.panic("unexpected errno: {d} ({t})", .{ @intFromEnum(e), e }); } else unreachable, } @@ -1662,22 +1662,12 @@ pub fn name_to_handle_atZ( pub const lfs64_abi = native_os == .linux and builtin.link_libc and (builtin.abi.isGnu() or builtin.abi.isAndroid()); -/// Whether or not `error.Unexpected` will print its value and a stack trace. -/// -/// If this happens the fix is to add the error code to the corresponding -/// switch expression, possibly introduce a new error in the error set, and -/// send a patch to Zig. -pub const unexpected_error_tracing = builtin.mode == .Debug and switch (builtin.zig_backend) { - .stage2_llvm, .stage2_x86_64 => true, - else => false, -}; - pub const UnexpectedError = std.Io.UnexpectedError; /// Call this when you made a syscall or something that sets errno /// and you get an unexpected error. pub fn unexpectedErrno(err: E) UnexpectedError { - if (unexpected_error_tracing) { + if (std.options.unexpected_error_tracing) { std.debug.print("unexpected errno: {d}\n", .{@intFromEnum(err)}); std.debug.dumpCurrentStackTrace(.{}); } diff --git a/lib/std/std.zig b/lib/std/std.zig index e700c728c10fd811a18cef3b826d3b2296af685d..a7b7daf6646923ea7e002a25157113ee807c7201 100644 --- a/lib/std/std.zig +++ b/lib/std/std.zig @@ -180,6 +180,16 @@ pub const Options = struct { /// Allows disabling networking in std.Io implementations. networking: bool = true, + /// Whether or not `error.Unexpected` will print its value and a stack trace. + /// + /// If this happens the fix is to add the error code to the corresponding + /// switch expression, possibly introduce a new error in the error set, and + /// send a patch to Zig. + unexpected_error_tracing: bool = @import("builtin").mode == .Debug and switch (@import("builtin").zig_backend) { + .stage2_llvm, .stage2_x86_64 => true, + else => false, + }, + /// TODO This is a separate decl instead of a field as a workaround around /// compilation errors due to zig not being lazy enough. pub const logTerminalMode: fn () Io.Terminal.Mode = log.defaultTerminalMode; diff --git a/src/link/MachO.zig b/src/link/MachO.zig index a92522d93d1313ea8e6b6525f971c10bc5cbaea9..bbd26fd762e06ee8841fcc532d33f94b265dee0b 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -5133,7 +5133,7 @@ pub fn getKernError(err: std.c.kern_return_t) KernE { } pub fn unexpectedKernError(err: KernE) std.posix.UnexpectedError { - if (std.posix.unexpected_error_tracing) { + if (std.options.unexpected_error_tracing) { std.debug.print("unexpected error: {d}\n", .{@intFromEnum(err)}); std.debug.dumpCurrentStackTrace(.{}); } -- 2.54.0