authorgravatar for mason@gamesbymason.comMason Remaley <mason@gamesbymason.com> 2026-04-09 15:43:05-07:00
committergravatar for mason@gamesbymason.comMason Remaley <mason@gamesbymason.com> 2026-04-12 04:01:29-07:00
logc2cbb944ba377db141e3dc5a890d955932accea2
tree8bf3852032934f69c0a50ca97dbf465d7aa7c8d2
parent6bf583c4baf6b33166176f03e7f570bbd4607a8b

Further improvements to stack trace type


2 files changed, 67 insertions(+), 32 deletions(-)

lib/std/debug.zig+62-27
......@@ -609,8 +609,33 @@ fn waitForOtherThreadToFinishPanicking() void {
609609/// This data structure is used by the Zig language code generation and
610610/// therefore must be kept in sync with the compiler implementation.
611611pub const StackTrace = struct {
612 index: usize,
612 /// Each element is the "return address" of a function call, meaning the instruction address
613 /// which control flow will return to when the function returns.
614 ///
615 /// The first slice element corresponds to the innermost stack frame, and the last element to
616 /// the outermost.
617 ///
618 /// Inlined function calls do not have meaningful return addresses and are therefore not
619 /// included in this slice. Instead, when printing the stack trace, the source locations of
620 /// inline calls should be read from debug information and the corresponding "inline frames"
621 /// printed in the appropriate locations.
613622 return_addresses: []usize,
623 /// Indicates whether any stack frames were omitted from `return_addresses`.
624 skipped: SkippedAddresses,
625
626};
627
628/// Indicates how many addresses were skipped in a trace.
629pub const SkippedAddresses = enum(usize) {
630 /// No addresses were omitted: `return_addresses` contains all stack frames, including the
631 /// outermost.
632 none = 0,
633 /// It is not known whether any frames were omitted.
634 unknown = std.math.maxInt(usize),
635 /// The full stack trace was available, but some frames are not included in
636 /// `return_addresses` due to buffer size limitations. The enum value is the exact number of
637 /// addresses which were omitted.
638 _,
614639};
615640
616641pub const StackUnwindOptions = struct {
......@@ -633,8 +658,8 @@ pub const StackUnwindOptions = struct {
633658/// See `writeCurrentStackTrace` to immediately print the trace instead of capturing it.
634659pub noinline fn captureCurrentStackTrace(options: StackUnwindOptions, addr_buf: []usize) StackTrace {
635660 const empty_trace: StackTrace = .{
636 .index = 0,
637661 .return_addresses = &.{},
662 .skipped = .none,
638663 };
639664 if (!std.options.allow_stack_tracing) return empty_trace;
640665 var it: StackIterator = .init(options.context);
......@@ -646,17 +671,17 @@ pub noinline fn captureCurrentStackTrace(options: StackUnwindOptions, addr_buf:
646671 var total_frames: usize = 0;
647672 var index: usize = 0;
648673 var wait_for = options.first_address;
649 // Ideally, we would iterate the whole stack so that the `index` in the returned trace was
674 // Ideally, we would iterate the whole stack so that the `index - min(buf.len, index)` would be
650675 // indicative of how many frames were skipped. However, this has a significant runtime cost
651676 // in some cases, so at least for now, we don't do that.
652 while (index < addr_buf.len) switch (it.next(io)) {
653 .switch_to_fp => if (!it.stratOk(options.allow_unsafe_unwind)) break,
654 .end => break,
677 const skipped: SkippedAddresses = while (index < addr_buf.len) switch (it.next(io)) {
678 .switch_to_fp => if (!it.stratOk(options.allow_unsafe_unwind)) break .unknown,
679 .end => break .none,
655680 .frame => |ret_addr| {
656681 if (total_frames > 10_000) {
657682 // Limit the number of frames in case of (e.g.) broken debug information which is
658683 // getting unwinding stuck in a loop.
659 break;
684 break .unknown;
660685 }
661686 total_frames += 1;
662687 if (wait_for) |target| {
......@@ -666,10 +691,10 @@ pub noinline fn captureCurrentStackTrace(options: StackUnwindOptions, addr_buf:
666691 addr_buf[index] = ret_addr;
667692 index += 1;
668693 },
669 };
694 } else .unknown;
670695 return .{
671 .index = index,
672696 .return_addresses = addr_buf[0..index],
697 .skipped = skipped,
673698 };
674699}
675700/// Write the current stack trace to `writer`, annotated with source locations.
......@@ -792,19 +817,21 @@ pub const FormatStackTrace = struct {
792817
793818/// Write a previously captured error return trace to `writer`, annotated with source locations.
794819pub fn writeErrorReturnTrace(et: *const std.builtin.ErrorReturnTrace, t: Io.Terminal) Writer.Error!void {
795 // Fetch `et.index` straight away. Aside from avoiding redundant loads, this prevents issues if
796 // errors are encountered while writing the stack trace.
797 try writeTrace(et.instruction_addresses, et.index, t, false);
820 // We take the slice by value, preventing the length from being mutated if an error occurs while
821 // writing the stack trace.
822 const len = @min(et.instruction_addresses.len, et.index);
823 const skipped = et.index - len;
824 try writeTrace(et.instruction_addresses[0..len], @enumFromInt(skipped), t, false);
798825}
799826
800827/// Write a previously captured stack trace to `writer`, annotated with source locations.
801828pub fn writeStackTrace(st: *const StackTrace, t: Io.Terminal) Writer.Error!void {
802 try writeTrace(st.return_addresses, st.index, t, true);
829 try writeTrace(st.return_addresses, st.skipped, t, true);
803830}
804831
805832fn writeTrace(
806833 addresses: []const usize,
807 n_frames: usize,
834 skipped: SkippedAddresses,
808835 t: Io.Terminal,
809836 resolve_inline_callers: bool,
810837) Writer.Error!void {
......@@ -816,7 +843,7 @@ fn writeTrace(
816843 return;
817844 }
818845
819 if (n_frames == 0) return writer.writeAll("(empty stack trace)\n");
846 if (addresses.len == 0) return writer.writeAll("(empty stack trace)\n");
820847 const di = getSelfDebugInfo() catch |err| switch (err) {
821848 error.UnsupportedTarget => {
822849 t.setColor(.dim) catch {};
......@@ -826,19 +853,26 @@ fn writeTrace(
826853 },
827854 };
828855 const io = std.Options.debug_io;
829 const captured_frames = @min(n_frames, addresses.len);
830 for (addresses[0..captured_frames]) |ret_addr| {
831 // `ret_addr` is the return address, which is *after* the function call.
856 for (addresses) |addr| {
857 // `addr` is the return address, which is *after* the function call.
832858 // Subtract 1 to get an address *in* the function call for a better source location.
833859 try printSourceAtAddress(io, di, t, .{
834 .address = ret_addr -| StackIterator.ra_call_offset,
860 .address = addr -| StackIterator.ra_call_offset,
835861 .resolve_inline_callers = resolve_inline_callers,
836862 });
837863 }
838 if (n_frames > captured_frames) {
839 t.setColor(.bold) catch {};
840 try writer.print("({d} additional stack frames skipped...)\n", .{n_frames - captured_frames});
841 t.setColor(.reset) catch {};
864 switch (skipped) {
865 .none => {},
866 .unknown => {
867 t.setColor(.bold) catch {};
868 try writer.writeAll("(additional stack frames may have been skipped...)\n");
869 t.setColor(.reset) catch {};
870 },
871 else => |n| {
872 t.setColor(.bold) catch {};
873 try writer.print("({d} additional stack frames skipped due to buffer size limitations...)\n", .{n});
874 t.setColor(.reset) catch {};
875 },
842876 }
843877}
844878/// A thin wrapper around `writeStackTrace` which writes to stderr and ignores write errors.
......@@ -1712,8 +1746,8 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize
17121746 t.notes[t.index] = note;
17131747 const addrs = &t.addrs[t.index];
17141748 const st = captureCurrentStackTrace(.{ .first_address = addr }, addrs);
1715 if (st.index < addrs.len) {
1716 @memset(addrs[st.index..], 0); // zero unused frames to indicate end of trace
1749 if (st.return_addresses.len < addrs.len) {
1750 @memset(addrs[st.return_addresses.len..], 0); // zero unused frames to indicate end of trace
17171751 }
17181752 }
17191753 // Keep counting even if the end is reached so that the
......@@ -1731,9 +1765,10 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize
17311765 stderr.writer.print("{s}:\n", .{t.notes[i]}) catch return;
17321766 var frames_array_mutable = frames_array;
17331767 const frames = mem.sliceTo(frames_array_mutable[0..], 0);
1768 const len = @min(t.index, frames.len);
17341769 const stack_trace: StackTrace = .{
1735 .index = frames.len,
1736 .return_addresses = frames,
1770 .return_addresses = frames[0..len],
1771 .skipped = if (len < frames.len) .none else .unknown,
17371772 };
17381773 writeStackTrace(&stack_trace, stderr) catch return;
17391774 }
lib/std/heap/debug_allocator.zig+5-5
......@@ -237,8 +237,8 @@ pub fn DebugAllocator(comptime config: Config) type {
237237 len += 1;
238238 }
239239 return .{
240 .return_addresses = stack_addresses,
241 .index = len,
240 .return_addresses = stack_addresses[0..len],
241 .skipped = if (len < stack_addresses.len) .none else .unknown,
242242 };
243243 }
244244
......@@ -339,8 +339,8 @@ pub fn DebugAllocator(comptime config: Config) type {
339339 len += 1;
340340 }
341341 return .{
342 .return_addresses = stack_addresses,
343 .index = len,
342 .return_addresses = stack_addresses[0..len],
343 .skipped = if (len < stack_addresses.len) .none else .unknown,
344344 };
345345 }
346346
......@@ -508,7 +508,7 @@ pub fn DebugAllocator(comptime config: Config) type {
508508
509509 fn collectStackTrace(first_trace_addr: usize, addr_buf: *[stack_n]usize) void {
510510 const st = std.debug.captureCurrentStackTrace(.{ .first_address = first_trace_addr }, addr_buf);
511 @memset(addr_buf[@min(st.index, addr_buf.len)..], 0);
511 @memset(addr_buf[@min(st.return_addresses.len, addr_buf.len)..], 0);
512512 }
513513
514514 fn reportDoubleFree(ret_addr: usize, alloc_stack_trace: StackTrace, free_stack_trace: StackTrace) void {