This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.
pubnoinlinefncaptureCurrentStackTrace(options: StackUnwindOptions, addr_buf: []usize) StackTrace {
constempty_trace: StackTrace = .{
.return_addresses = &.{},
.skipped = .none,
};
if (!std.options.allow_stack_tracing) returnempty_trace;
varit: StackIterator = .init(options.context);
deferit.deinit();
if (!it.stratOk(options.allow_unsafe_unwind)) returnempty_trace;
constio = std.Options.debug_io;
vartotal_frames: usize = 0;
varindex: usize = 0;
varwait_for = options.first_address;
// Ideally, we would iterate the whole stack so that the `index - min(buf.len, index)` would be
// indicative of how many frames were skipped. However, this has a significant runtime cost
// in some cases, so at least for now, we don't do that.
constskipped: SkippedAddresses = while (index < addr_buf.len) switch (it.next(io)) {
.switch_to_fp => if (!it.stratOk(options.allow_unsafe_unwind)) break .unknown,
.end => break .none,
.frame => |ret_addr| {
if (total_frames > 10_000) {
// Limit the number of frames in case of (e.g.) broken debug information which is
// getting unwinding stuck in a loop.
break .unknown;
}
total_frames += 1;
if (wait_for) |target| {
if (ret_addr != target) continue;
wait_for = null;
}
addr_buf[index] = ret_addr;
index += 1;
},
} else .unknown;
return .{
.return_addresses = addr_buf[0..index],
.skipped = skipped,
};
}