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
This commit is contained in:
David Gonzalez Martin
2026-04-13 12:17:51 +02:00
committed by Andrew Kelley
parent c457939f10
commit 37a20d3984
4 changed files with 15 additions and 15 deletions
+2 -2
View File
@@ -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 "<unnamed>",
+2 -12
View File
@@ -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(.{});
}
+10
View File
@@ -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;
+1 -1
View File
@@ -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(.{});
}