zig/test/standalone/stack_iterator/shared_lib_unwind.zig
Alex Rønne Petersen 481b7bf3f0
std.Target: Remove functions that just wrap component functions.
Functions like isMinGW() and isGnuLibC() have a good reason to exist: They look
at multiple components of the target. But functions like isWasm(), isDarwin(),
isGnu(), etc only exist to save 4-8 characters. I don't think this is a good
enough reason to keep them, especially given that:

* It's not immediately obvious to a reader whether target.isDarwin() means the
  same thing as target.os.tag.isDarwin() precisely because isMinGW() and similar
  functions *do* look at multiple components.
* It's not clear where we would draw the line. The logical conclusion before
  this commit would be to also wrap Arch.isX86(), Os.Tag.isSolarish(),
  Abi.isOpenHarmony(), etc... this obviously quickly gets out of hand.
* It's nice to just have a single correct way of doing something.
2025-02-17 19:18:19 +01:00

49 lines
1.7 KiB
Zig

const std = @import("std");
const builtin = @import("builtin");
const debug = std.debug;
const testing = std.testing;
noinline fn frame4(expected: *[5]usize, unwound: *[5]usize) void {
expected[0] = @returnAddress();
var context: debug.ThreadContext = undefined;
testing.expect(debug.getContext(&context)) catch @panic("failed to getContext");
const debug_info = debug.getSelfDebugInfo() catch @panic("failed to openSelfDebugInfo");
var it = debug.StackIterator.initWithContext(expected[0], debug_info, &context) catch @panic("failed to initWithContext");
defer it.deinit();
for (unwound) |*addr| {
if (it.next()) |return_address| addr.* = return_address;
}
}
noinline fn frame3(expected: *[5]usize, unwound: *[5]usize) void {
expected[1] = @returnAddress();
frame4(expected, unwound);
}
fn frame2(expected: *[5]usize, unwound: *[5]usize) callconv(.C) void {
expected[2] = @returnAddress();
frame3(expected, unwound);
}
extern fn frame0(
expected: *[5]usize,
unwound: *[5]usize,
frame_2: *const fn (expected: *[5]usize, unwound: *[5]usize) callconv(.C) void,
) void;
pub fn main() !void {
// Disabled until the DWARF unwinder bugs on .aarch64 are solved
if (builtin.omit_frame_pointer and comptime builtin.target.os.tag.isDarwin() and builtin.cpu.arch == .aarch64) return;
if (builtin.target.os.tag.isDarwin() and builtin.cpu.arch == .x86_64) return; // https://github.com/ziglang/zig/issues/21337
if (!std.debug.have_ucontext or !std.debug.have_getcontext) return;
var expected: [5]usize = undefined;
var unwound: [5]usize = undefined;
frame0(&expected, &unwound, &frame2);
try testing.expectEqual(expected, unwound);
}