authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-02-12 12:59:22-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-02-12 13:13:45-05:00
log251f54d1d7d0f871f551c54d77f1a8238a041983
tree83f7a9ab9268a57185f8d986a65f35e6a3e33ae6
parent381e23146809b90de5a32f0fe1548c00b06e8ab2

crash_report: finish reverting panic changes


2 files changed, 40 insertions(+), 26 deletions(-)

src/crash_report.zig+35-22
......@@ -1,14 +1,31 @@
1/// We override the panic implementation to our own one, so we can print our own information before
2/// calling the default panic handler. This declaration must be re-exposed from `@import("root")`.
3pub const panic = std.debug.FullPanic(panicImpl);
4
5/// We let std install its segfault handler, but we override the target-agnostic handler it calls,
6/// so we can print our own information before calling the default segfault logic. This declaration
7/// must be re-exposed from `@import("root")`.
8pub const debug = struct {
9 pub const handleSegfault = handleSegfaultImpl;
10};
11
112/// Printed in panic messages when suggesting a command to run, allowing copy-pasting the command.
213/// Set by `main` as soon as arguments are known. The value here is a default in case we somehow
314/// crash earlier than that.
415pub var zig_argv0: []const u8 = "zig";
516
6const enabled = switch (build_options.io_mode) {
7 .threaded => build_options.enable_debug_extensions,
8 .evented => false, // would use threadlocals in a way incompatible with evented
9};
17fn handleSegfaultImpl(addr: ?usize, name: []const u8, opt_ctx: ?std.debug.CpuContextPtr) noreturn {
18 @branchHint(.cold);
19 dumpCrashContext() catch {};
20 std.debug.defaultHandleSegfault(addr, name, opt_ctx);
21}
22fn panicImpl(msg: []const u8, first_trace_addr: ?usize) noreturn {
23 @branchHint(.cold);
24 dumpCrashContext() catch {};
25 std.debug.defaultPanic(msg, first_trace_addr orelse @returnAddress());
26}
1027
11pub const AnalyzeBody = if (enabled) struct {
28pub const AnalyzeBody = struct {
1229 parent: ?*AnalyzeBody,
1330 sema: *Sema,
1431 block: *Sema.Block,
......@@ -35,15 +52,9 @@ pub const AnalyzeBody = if (enabled) struct {
3552 std.debug.assert(current.? == ab); // `Sema.analyzeBodyInner` did not match push/pop calls
3653 current = ab.parent;
3754 }
38} else struct {
39 const current: ?noreturn = null;
40 // Dummy implementation, with functions marked `inline` to avoid interfering with tail calls.
41 pub inline fn push(_: AnalyzeBody, _: *Sema, _: *Sema.Block, _: []const Zir.Inst.Index) void {}
42 pub inline fn pop(_: AnalyzeBody) void {}
43 pub inline fn setBodyIndex(_: @This(), _: usize) void {}
4455};
4556
46pub const CodegenFunc = if (enabled) struct {
57pub const CodegenFunc = struct {
4758 zcu: *const Zcu,
4859 func_index: InternPool.Index,
4960 threadlocal var current: ?CodegenFunc = null;
......@@ -55,21 +66,25 @@ pub const CodegenFunc = if (enabled) struct {
5566 std.debug.assert(current.?.func_index == func_index);
5667 current = null;
5768 }
58} else struct {
59 const current: ?noreturn = null;
60 // Dummy implementation
61 pub fn start(_: *const Zcu, _: InternPool.Index) void {}
62 pub fn stop(_: InternPool.Index) void {}
6369};
6470
65pub fn dumpCrashContext(terminal: Io.Terminal) Io.Writer.Error!void {
71fn dumpCrashContext() Io.Writer.Error!void {
6672 const S = struct {
73 /// In the case of recursive panics or segfaults, don't print the context for a second time.
74 threadlocal var already_dumped = false;
6775 /// TODO: make this unnecessary. It exists because `print_zir` currently needs an allocator,
6876 /// but that shouldn't be necessary---it's already only used in one place.
69 var crash_heap: [64 * 1024]u8 = undefined;
77 threadlocal var crash_heap: [64 * 1024]u8 = undefined;
7078 };
79 if (S.already_dumped) return;
80 S.already_dumped = true;
81
82 // TODO: this does mean that a different thread could grab the stderr mutex between the context
83 // and the actual panic printing, which would be quite confusing.
84 const stderr = std.debug.lockStderr(&.{});
85 defer std.debug.unlockStderr();
86 const w = &stderr.file_writer.interface;
7187
72 const w = terminal.writer;
7388 try w.writeAll("Compiler crash context:\n");
7489
7590 if (CodegenFunc.current) |*cg| {
......@@ -155,5 +170,3 @@ const Zcu = @import("Zcu.zig");
155170const InternPool = @import("InternPool.zig");
156171const dev = @import("dev.zig");
157172const print_zir = @import("print_zir.zig");
158
159const build_options = @import("build_options");
src/main.zig+5-4
......@@ -52,11 +52,12 @@ pub const std_options: std.Options = .{
5252};
5353pub const std_options_cwd = if (native_os == .wasi) wasi_cwd else null;
5454
55pub const debug = struct {
56 pub fn printCrashContext(terminal: Io.Terminal) void {
57 crash_report.dumpCrashContext(terminal) catch {};
58 }
55const crash_report_enabled = switch (build_options.io_mode) {
56 .threaded => build_options.enable_debug_extensions,
57 .evented => false, // would use threadlocals in a way incompatible with evented
5958};
59pub const panic = if (crash_report_enabled) crash_report.panic else std.debug.FullPanic(std.debug.defaultPanic);
60pub const debug = if (crash_report_enabled) crash_report.debug else struct {};
6061
6162var preopens: std.process.Preopens = .empty;
6263pub fn wasi_cwd() Io.Dir {