authorgravatar for davidgmbb@gmail.comDavid Gonzalez Martin <davidgmbb@gmail.com> 2026-04-13 12:17:51+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-04-13 19:58:01+02:00
log37a20d39846b9056dbf6aa46cb2bed3196b56b61
tree17600698ce91281031d512267eabf4f33b182104
parentc457939f104595d675974f9e820ccf4187bf8e95

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

4 files changed, 15 insertions(+), 15 deletions(-)

lib/std/os/windows.zig+2-2
...@@ -3952,7 +3952,7 @@ inline fn MAKELANGID(p: c_ushort, s: c_ushort) LANGID {...@@ -3952,7 +3952,7 @@ inline fn MAKELANGID(p: c_ushort, s: c_ushort) LANGID {
3952/// and you get an unexpected error.3952/// and you get an unexpected error.
3953pub fn unexpectedError(err: Win32Error) UnexpectedError {3953pub fn unexpectedError(err: Win32Error) UnexpectedError {
3954 @branchHint(.cold);3954 @branchHint(.cold);
3955 if (std.posix.unexpected_error_tracing) {3955 if (std.options.unexpected_error_tracing) {
3956 std.debug.print("error.Unexpected: GetLastError({d}): {t}\n", .{ err, err });3956 std.debug.print("error.Unexpected: GetLastError({d}): {t}\n", .{ err, err });
3957 std.debug.dumpCurrentStackTrace(.{ .first_address = @returnAddress() });3957 std.debug.dumpCurrentStackTrace(.{ .first_address = @returnAddress() });
3958 }3958 }
...@@ -3962,7 +3962,7 @@ pub fn unexpectedError(err: Win32Error) UnexpectedError {...@@ -3962,7 +3962,7 @@ pub fn unexpectedError(err: Win32Error) UnexpectedError {
3962/// Call this when you made a windows NtDll call3962/// Call this when you made a windows NtDll call
3963/// and you get an unexpected status.3963/// and you get an unexpected status.
3964pub fn unexpectedStatus(status: NTSTATUS) UnexpectedError {3964pub fn unexpectedStatus(status: NTSTATUS) UnexpectedError {
3965 if (std.posix.unexpected_error_tracing) {3965 if (std.options.unexpected_error_tracing) {
3966 std.debug.print("error.Unexpected NTSTATUS=0x{x} ({s})\n", .{3966 std.debug.print("error.Unexpected NTSTATUS=0x{x} ({s})\n", .{
3967 @intFromEnum(status),3967 @intFromEnum(status),
3968 std.enums.tagName(NTSTATUS, status) orelse "<unnamed>",3968 std.enums.tagName(NTSTATUS, status) orelse "<unnamed>",
lib/std/posix.zig+2-12
...@@ -686,7 +686,7 @@ pub fn munmap(memory: []align(page_size_min) const u8) void {...@@ -686,7 +686,7 @@ pub fn munmap(memory: []align(page_size_min) const u8) void {
686 .SUCCESS => return,686 .SUCCESS => return,
687 .INVAL => unreachable, // Invalid parameters.687 .INVAL => unreachable, // Invalid parameters.
688 .NOMEM => unreachable, // Attempted to unmap a region in the middle of an existing mapping.688 .NOMEM => unreachable, // Attempted to unmap a region in the middle of an existing mapping.
689 else => |e| if (unexpected_error_tracing) {689 else => |e| if (std.options.unexpected_error_tracing) {
690 std.debug.panic("unexpected errno: {d} ({t})", .{ @intFromEnum(e), e });690 std.debug.panic("unexpected errno: {d} ({t})", .{ @intFromEnum(e), e });
691 } else unreachable,691 } else unreachable,
692 }692 }
...@@ -1662,22 +1662,12 @@ pub fn name_to_handle_atZ(...@@ -1662,22 +1662,12 @@ pub fn name_to_handle_atZ(
16621662
1663pub const lfs64_abi = native_os == .linux and builtin.link_libc and (builtin.abi.isGnu() or builtin.abi.isAndroid());1663pub const lfs64_abi = native_os == .linux and builtin.link_libc and (builtin.abi.isGnu() or builtin.abi.isAndroid());
16641664
1665/// Whether or not `error.Unexpected` will print its value and a stack trace.
1666///
1667/// If this happens the fix is to add the error code to the corresponding
1668/// switch expression, possibly introduce a new error in the error set, and
1669/// send a patch to Zig.
1670pub const unexpected_error_tracing = builtin.mode == .Debug and switch (builtin.zig_backend) {
1671 .stage2_llvm, .stage2_x86_64 => true,
1672 else => false,
1673};
1674
1675pub const UnexpectedError = std.Io.UnexpectedError;1665pub const UnexpectedError = std.Io.UnexpectedError;
16761666
1677/// Call this when you made a syscall or something that sets errno1667/// Call this when you made a syscall or something that sets errno
1678/// and you get an unexpected error.1668/// and you get an unexpected error.
1679pub fn unexpectedErrno(err: E) UnexpectedError {1669pub fn unexpectedErrno(err: E) UnexpectedError {
1680 if (unexpected_error_tracing) {1670 if (std.options.unexpected_error_tracing) {
1681 std.debug.print("unexpected errno: {d}\n", .{@intFromEnum(err)});1671 std.debug.print("unexpected errno: {d}\n", .{@intFromEnum(err)});
1682 std.debug.dumpCurrentStackTrace(.{});1672 std.debug.dumpCurrentStackTrace(.{});
1683 }1673 }
lib/std/std.zig+10
...@@ -180,6 +180,16 @@ pub const Options = struct {...@@ -180,6 +180,16 @@ pub const Options = struct {
180 /// Allows disabling networking in std.Io implementations.180 /// Allows disabling networking in std.Io implementations.
181 networking: bool = true,181 networking: bool = true,
182182
183 /// Whether or not `error.Unexpected` will print its value and a stack trace.
184 ///
185 /// If this happens the fix is to add the error code to the corresponding
186 /// switch expression, possibly introduce a new error in the error set, and
187 /// send a patch to Zig.
188 unexpected_error_tracing: bool = @import("builtin").mode == .Debug and switch (@import("builtin").zig_backend) {
189 .stage2_llvm, .stage2_x86_64 => true,
190 else => false,
191 },
192
183 /// TODO This is a separate decl instead of a field as a workaround around193 /// TODO This is a separate decl instead of a field as a workaround around
184 /// compilation errors due to zig not being lazy enough.194 /// compilation errors due to zig not being lazy enough.
185 pub const logTerminalMode: fn () Io.Terminal.Mode = log.defaultTerminalMode;195 pub const logTerminalMode: fn () Io.Terminal.Mode = log.defaultTerminalMode;
src/link/MachO.zig+1-1
...@@ -5133,7 +5133,7 @@ pub fn getKernError(err: std.c.kern_return_t) KernE {...@@ -5133,7 +5133,7 @@ pub fn getKernError(err: std.c.kern_return_t) KernE {
5133}5133}
51345134
5135pub fn unexpectedKernError(err: KernE) std.posix.UnexpectedError {5135pub fn unexpectedKernError(err: KernE) std.posix.UnexpectedError {
5136 if (std.posix.unexpected_error_tracing) {5136 if (std.options.unexpected_error_tracing) {
5137 std.debug.print("unexpected error: {d}\n", .{@intFromEnum(err)});5137 std.debug.print("unexpected error: {d}\n", .{@intFromEnum(err)});
5138 std.debug.dumpCurrentStackTrace(.{});5138 std.debug.dumpCurrentStackTrace(.{});
5139 }5139 }