mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
Previously, the `test-stack-traces` step was essentially just testing error traces, and even there we didn't have much coverage. This commit solves that by splitting the "stack trace" tests into two separate harnesses: the "stack trace" tests are for actual stack traces (i.e. involving stack unwinding), while the "error trace" tests are specifically for error return traces. The "stack trace" tests will test different configurations of: * `-lc` * `-fPIE` * `-fomit-frame-pointer` * `-fllvm` * unwind tables (currently disabled) * strip debug info (currently disabled) The main goal there is to test *stack unwinding* under different conditions. Meanwhile, the "error trace" tests will test different configurations of `-O` and `-fllvm`; the main goal here, aside from checking that error traces themselves do not miscompile, is to check whether debug info is still working even in optimized builds. Of course, aggressive optimizations *can* thwart debug info no matter what, so as before, there is a way to disable cases for specific targets / optimize modes. The program which converts stack traces into a more validatable format by removing things like addresses (previously `check-stack-trace.zig`, now `convert-stack-trace.zig`) has been rewritten and simplified. Also, thanks to various fixes in this branch, several workarounds have become unnecessary: for instance, we don't need to ignore the function name printed in stack traces in release modes, because `std.debug.Dwarf` now uses the correct DIE for inlined functions! Neither `test-stack-traces` nor `test-error-traces` does general foreign architecture testing, because it seems that (at least for now) external executors often aren't particularly good at handling stack tracing correctly (looking at you, Wine). Generally, they just test the native target (this matches the old behavior of `test-stack-traces`). However, there is one exception: when on an x86_64 or aarch64 host, we will also test the 32-bit version (x86 or arm) if the OS supports it, because such executables can be trivially tested without an external executor. Oh, also, I wrote a bunch of stack trace tests. Previously there was, erm, *one* test in `test-stack-traces` which wasn't for error traces. Now there are a good few!
224 lines
6.5 KiB
Zig
224 lines
6.5 KiB
Zig
pub fn addCases(cases: *@import("tests.zig").StackTracesContext) void {
|
|
cases.addCase(.{
|
|
.name = "simple panic",
|
|
.source =
|
|
\\pub fn main() void {
|
|
\\ foo();
|
|
\\}
|
|
\\fn foo() void {
|
|
\\ @panic("oh no");
|
|
\\}
|
|
\\
|
|
,
|
|
.unwind = .any,
|
|
.expect_panic = true,
|
|
.expect =
|
|
\\panic: oh no
|
|
\\source.zig:5:5: [address] in foo
|
|
\\ @panic("oh no");
|
|
\\ ^
|
|
\\source.zig:2:8: [address] in main
|
|
\\ foo();
|
|
\\ ^
|
|
\\
|
|
,
|
|
.expect_strip =
|
|
\\panic: oh no
|
|
\\???:?:?: [address] in source.foo
|
|
\\???:?:?: [address] in source.main
|
|
\\
|
|
,
|
|
});
|
|
|
|
cases.addCase(.{
|
|
.name = "simple panic with no unwind strategy",
|
|
.source =
|
|
\\pub fn main() void {
|
|
\\ foo();
|
|
\\}
|
|
\\fn foo() void {
|
|
\\ @panic("oh no");
|
|
\\}
|
|
\\
|
|
,
|
|
.unwind = .none,
|
|
.expect_panic = true,
|
|
.expect = "panic: oh no",
|
|
.expect_strip = "panic: oh no",
|
|
});
|
|
|
|
cases.addCase(.{
|
|
.name = "dump current trace",
|
|
.source =
|
|
\\pub fn main() void {
|
|
\\ foo(bar());
|
|
\\}
|
|
\\fn bar() void {
|
|
\\ qux(123);
|
|
\\}
|
|
\\fn foo(_: void) void {}
|
|
\\fn qux(x: u32) void {
|
|
\\ std.debug.dumpCurrentStackTrace(.{});
|
|
\\ _ = x;
|
|
\\}
|
|
\\const std = @import("std");
|
|
\\
|
|
,
|
|
.unwind = .safe,
|
|
.expect_panic = false,
|
|
.expect =
|
|
\\source.zig:9:36: [address] in qux
|
|
\\ std.debug.dumpCurrentStackTrace(.{});
|
|
\\ ^
|
|
\\source.zig:5:8: [address] in bar
|
|
\\ qux(123);
|
|
\\ ^
|
|
\\source.zig:2:12: [address] in main
|
|
\\ foo(bar());
|
|
\\ ^
|
|
\\
|
|
,
|
|
.expect_strip =
|
|
\\???:?:?: [address] in source.qux
|
|
\\???:?:?: [address] in source.bar
|
|
\\???:?:?: [address] in source.main
|
|
\\
|
|
,
|
|
});
|
|
|
|
cases.addCase(.{
|
|
.name = "dump current trace with no unwind strategy",
|
|
.source =
|
|
\\pub fn main() void {
|
|
\\ foo(bar());
|
|
\\}
|
|
\\fn bar() void {
|
|
\\ qux(123);
|
|
\\}
|
|
\\fn foo(_: void) void {}
|
|
\\fn qux(x: u32) void {
|
|
\\ std.debug.print("pre\n", .{});
|
|
\\ std.debug.dumpCurrentStackTrace(.{});
|
|
\\ std.debug.print("post\n", .{});
|
|
\\ _ = x;
|
|
\\}
|
|
\\const std = @import("std");
|
|
\\
|
|
,
|
|
.unwind = .no_safe,
|
|
.expect_panic = false,
|
|
.expect = "pre\npost\n",
|
|
.expect_strip = "pre\npost\n",
|
|
});
|
|
|
|
cases.addCase(.{
|
|
.name = "dump captured trace",
|
|
.source =
|
|
\\pub fn main() void {
|
|
\\ var stack_trace_buf: [8]usize = undefined;
|
|
\\ dumpIt(&captureIt(&stack_trace_buf));
|
|
\\}
|
|
\\fn captureIt(buf: []usize) std.builtin.StackTrace {
|
|
\\ return captureItInner(buf);
|
|
\\}
|
|
\\fn dumpIt(st: *const std.builtin.StackTrace) void {
|
|
\\ std.debug.dumpStackTrace(st);
|
|
\\}
|
|
\\fn captureItInner(buf: []usize) std.builtin.StackTrace {
|
|
\\ return std.debug.captureCurrentStackTrace(.{}, buf);
|
|
\\}
|
|
\\const std = @import("std");
|
|
\\
|
|
,
|
|
.unwind = .safe,
|
|
.expect_panic = false,
|
|
.expect =
|
|
\\source.zig:12:46: [address] in captureItInner
|
|
\\ return std.debug.captureCurrentStackTrace(.{}, buf);
|
|
\\ ^
|
|
\\source.zig:6:26: [address] in captureIt
|
|
\\ return captureItInner(buf);
|
|
\\ ^
|
|
\\source.zig:3:22: [address] in main
|
|
\\ dumpIt(&captureIt(&stack_trace_buf));
|
|
\\ ^
|
|
\\
|
|
,
|
|
.expect_strip =
|
|
\\???:?:?: [address] in source.captureItInner
|
|
\\???:?:?: [address] in source.captureIt
|
|
\\???:?:?: [address] in source.main
|
|
\\
|
|
,
|
|
});
|
|
|
|
cases.addCase(.{
|
|
.name = "dump captured trace with no unwind strategy",
|
|
.source =
|
|
\\pub fn main() void {
|
|
\\ var stack_trace_buf: [8]usize = undefined;
|
|
\\ dumpIt(&captureIt(&stack_trace_buf));
|
|
\\}
|
|
\\fn captureIt(buf: []usize) std.builtin.StackTrace {
|
|
\\ return captureItInner(buf);
|
|
\\}
|
|
\\fn dumpIt(st: *const std.builtin.StackTrace) void {
|
|
\\ std.debug.dumpStackTrace(st);
|
|
\\}
|
|
\\fn captureItInner(buf: []usize) std.builtin.StackTrace {
|
|
\\ return std.debug.captureCurrentStackTrace(.{}, buf);
|
|
\\}
|
|
\\const std = @import("std");
|
|
\\
|
|
,
|
|
.unwind = .no_safe,
|
|
.expect_panic = false,
|
|
.expect = "(empty stack trace)\n",
|
|
.expect_strip = "(empty stack trace)\n",
|
|
});
|
|
|
|
cases.addCase(.{
|
|
.name = "dump captured trace on thread",
|
|
.source =
|
|
\\pub fn main() !void {
|
|
\\ var stack_trace_buf: [8]usize = undefined;
|
|
\\ const t = try std.Thread.spawn(.{}, threadMain, .{&stack_trace_buf});
|
|
\\ t.join();
|
|
\\}
|
|
\\fn threadMain(stack_trace_buf: []usize) void {
|
|
\\ dumpIt(&captureIt(stack_trace_buf));
|
|
\\}
|
|
\\fn captureIt(buf: []usize) std.builtin.StackTrace {
|
|
\\ return captureItInner(buf);
|
|
\\}
|
|
\\fn dumpIt(st: *const std.builtin.StackTrace) void {
|
|
\\ std.debug.dumpStackTrace(st);
|
|
\\}
|
|
\\fn captureItInner(buf: []usize) std.builtin.StackTrace {
|
|
\\ return std.debug.captureCurrentStackTrace(.{}, buf);
|
|
\\}
|
|
\\const std = @import("std");
|
|
\\
|
|
,
|
|
.unwind = .safe,
|
|
.expect_panic = false,
|
|
.expect =
|
|
\\source.zig:16:46: [address] in captureItInner
|
|
\\ return std.debug.captureCurrentStackTrace(.{}, buf);
|
|
\\ ^
|
|
\\source.zig:10:26: [address] in captureIt
|
|
\\ return captureItInner(buf);
|
|
\\ ^
|
|
\\source.zig:7:22: [address] in threadMain
|
|
\\ dumpIt(&captureIt(stack_trace_buf));
|
|
\\ ^
|
|
\\
|
|
,
|
|
.expect_strip =
|
|
\\???:?:?: [address] in source.captureItInner
|
|
\\???:?:?: [address] in source.captureIt
|
|
\\???:?:?: [address] in source.threadMain
|
|
\\
|
|
,
|
|
});
|
|
}
|