fix compile errors and minor bugs

This commit is contained in:
mlugg 2025-09-13 10:29:20 +01:00
parent 5f00738969
commit 51d08f4b9b
No known key found for this signature in database
GPG Key ID: 3F5B7DCCBF4AF02E
5 changed files with 28 additions and 20 deletions

View File

@ -140,7 +140,7 @@ fn mainServer() !void {
else => { else => {
fail = true; fail = true;
if (@errorReturnTrace()) |trace| { if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*); std.debug.dumpStackTrace(trace);
} }
}, },
}; };
@ -182,7 +182,7 @@ fn mainServer() !void {
error.SkipZigTest => return, error.SkipZigTest => return,
else => { else => {
if (@errorReturnTrace()) |trace| { if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*); std.debug.dumpStackTrace(trace);
} }
std.debug.print("failed with error.{t}\n", .{err}); std.debug.print("failed with error.{t}\n", .{err});
std.process.exit(1); std.process.exit(1);
@ -261,7 +261,7 @@ fn mainTerminal() void {
std.debug.print("FAIL ({t})\n", .{err}); std.debug.print("FAIL ({t})\n", .{err});
} }
if (@errorReturnTrace()) |trace| { if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*); std.debug.dumpStackTrace(trace);
} }
test_node.end(); test_node.end();
}, },
@ -398,7 +398,7 @@ pub fn fuzz(
error.SkipZigTest => return, error.SkipZigTest => return,
else => { else => {
std.debug.lockStdErr(); std.debug.lockStdErr();
if (@errorReturnTrace()) |trace| std.debug.dumpStackTrace(trace.*); if (@errorReturnTrace()) |trace| std.debug.dumpStackTrace(trace);
std.debug.print("failed with error.{t}\n", .{err}); std.debug.print("failed with error.{t}\n", .{err});
std.process.exit(1); std.process.exit(1);
}, },

View File

@ -754,6 +754,10 @@ pub fn dumpCurrentStackTrace(options: StackUnwindOptions) void {
/// Write a previously captured stack trace to `writer`, annotated with source locations. /// Write a previously captured stack trace to `writer`, annotated with source locations.
pub fn writeStackTrace(st: *const std.builtin.StackTrace, writer: *Writer, tty_config: tty.Config) Writer.Error!void { pub fn writeStackTrace(st: *const std.builtin.StackTrace, writer: *Writer, tty_config: tty.Config) Writer.Error!void {
// Fetch `st.index` straight away. Aside from avoiding redundant loads, this prevents issues if
// `st` is `@errorReturnTrace()` and errors are encountered while writing the stack trace.
const n_frames = st.index;
if (n_frames == 0) return writer.writeAll("(empty stack trace)\n");
const di_gpa = getDebugInfoAllocator(); const di_gpa = getDebugInfoAllocator();
const di = getSelfDebugInfo() catch |err| switch (err) { const di = getSelfDebugInfo() catch |err| switch (err) {
error.UnsupportedTarget => { error.UnsupportedTarget => {
@ -763,14 +767,13 @@ pub fn writeStackTrace(st: *const std.builtin.StackTrace, writer: *Writer, tty_c
return; return;
}, },
}; };
if (st.index == 0) return writer.writeAll("(empty stack trace)\n"); const captured_frames = @min(n_frames, st.instruction_addresses.len);
const captured_frames = @min(st.index, st.instruction_addresses.len);
for (st.instruction_addresses[0..captured_frames]) |return_address| { for (st.instruction_addresses[0..captured_frames]) |return_address| {
try printSourceAtAddress(di_gpa, di, writer, return_address -| 1, tty_config); try printSourceAtAddress(di_gpa, di, writer, return_address -| 1, tty_config);
} }
if (st.index > captured_frames) { if (n_frames > captured_frames) {
tty_config.setColor(writer, .bold) catch {}; tty_config.setColor(writer, .bold) catch {};
try writer.print("({d} additional stack frames skipped...)\n", .{st.index - captured_frames}); try writer.print("({d} additional stack frames skipped...)\n", .{n_frames - captured_frames});
tty_config.setColor(writer, .reset) catch {}; tty_config.setColor(writer, .reset) catch {};
} }
} }
@ -853,7 +856,7 @@ const StackIterator = union(enum) {
const di = getSelfDebugInfo() catch unreachable; const di = getSelfDebugInfo() catch unreachable;
const di_gpa = getDebugInfoAllocator(); const di_gpa = getDebugInfoAllocator();
if (di.unwindFrame(di_gpa, unwind_context)) |ra| { if (di.unwindFrame(di_gpa, unwind_context)) |ra| {
if (ra == 0) return .end; if (ra <= 1) return .end;
return .{ .frame = ra }; return .{ .frame = ra };
} else |err| { } else |err| {
const pc = unwind_context.pc; const pc = unwind_context.pc;
@ -888,7 +891,9 @@ const StackIterator = union(enum) {
if (bp != 0 and bp <= fp) return .end; if (bp != 0 and bp <= fp) return .end;
it.fp = bp; it.fp = bp;
return .{ .frame = ra_ptr.* }; const ra = ra_ptr.*;
if (ra <= 1) return .end;
return .{ .frame = ra };
}, },
} }
} }
@ -1409,11 +1414,12 @@ test "manage resources correctly" {
return @returnAddress(); return @returnAddress();
} }
}; };
var discarding: std.io.Writer.Discarding = .init(&.{}); const gpa = std.testing.allocator;
var di: SelfInfo = try .open(testing.allocator); var discarding: std.Io.Writer.Discarding = .init(&.{});
defer di.deinit(); var di: SelfInfo = .init;
defer di.deinit(gpa);
try printSourceAtAddress( try printSourceAtAddress(
testing.allocator, gpa,
&di, &di,
&discarding.writer, &discarding.writer,
S.showMyTrace(), S.showMyTrace(),

View File

@ -18,8 +18,8 @@ const root = @import("root");
const SelfInfo = @This(); const SelfInfo = @This();
modules: std.AutoArrayHashMapUnmanaged(usize, Module.DebugInfo), modules: if (target_supported) std.AutoArrayHashMapUnmanaged(usize, Module.DebugInfo) else void,
lookup_cache: Module.LookupCache, lookup_cache: if (target_supported) Module.LookupCache else void,
pub const Error = error{ pub const Error = error{
/// The required debug info is invalid or corrupted. /// The required debug info is invalid or corrupted.
@ -40,7 +40,7 @@ pub const target_supported: bool = Module != void;
/// Indicates whether the `SelfInfo` implementation has support for unwinding on this target. /// Indicates whether the `SelfInfo` implementation has support for unwinding on this target.
/// ///
/// For whether DWARF unwinding is *theoretically* possible, see `Dwarf.abi.supportsUnwinding`. /// For whether DWARF unwinding is *theoretically* possible, see `Dwarf.abi.supportsUnwinding`.
pub const supports_unwinding: bool = Module.supports_unwinding; pub const supports_unwinding: bool = target_supported and Module.supports_unwinding;
pub const UnwindContext = if (supports_unwinding) Module.UnwindContext; pub const UnwindContext = if (supports_unwinding) Module.UnwindContext;

View File

@ -635,8 +635,10 @@ pub inline fn callMain() u8 {
else => {}, else => {},
} }
std.log.err("{s}", .{@errorName(err)}); std.log.err("{s}", .{@errorName(err)});
if (@errorReturnTrace()) |trace| { if (native_os != .freestanding) {
std.debug.dumpStackTrace(trace); if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace);
}
} }
return 1; return 1;
}; };

View File

@ -64,7 +64,7 @@ fn alloc(
const self: *FailingAllocator = @ptrCast(@alignCast(ctx)); const self: *FailingAllocator = @ptrCast(@alignCast(ctx));
if (self.alloc_index == self.fail_index) { if (self.alloc_index == self.fail_index) {
if (!self.has_induced_failure) { if (!self.has_induced_failure) {
const st = std.debug.captureCurrentStackTrace(return_address, &self.stack_addresses); const st = std.debug.captureCurrentStackTrace(.{ .first_address = return_address }, &self.stack_addresses);
@memset(self.stack_addresses[@min(st.index, self.stack_addresses.len)..], 0); @memset(self.stack_addresses[@min(st.index, self.stack_addresses.len)..], 0);
self.has_induced_failure = true; self.has_induced_failure = true;
} }