diff options
author | 2021-09-12 00:40:27 -0700 | |
---|---|---|
committer | 2021-09-12 00:40:27 -0700 | |
commit | 19922287aadc78ac8c2aa97885386d4d3366a366 (patch) | |
tree | d37703d3368f58882df7286a22552245b6eca8c7 /src/panic_handler.zig | |
parent | 5944c91de41191eb42b45edc2704d4c667327419 (diff) | |
download | bun-19922287aadc78ac8c2aa97885386d4d3366a366.tar.gz bun-19922287aadc78ac8c2aa97885386d4d3366a366.tar.zst bun-19922287aadc78ac8c2aa97885386d4d3366a366.zip |
Fix panic handler to not double panic
Diffstat (limited to 'src/panic_handler.zig')
-rw-r--r-- | src/panic_handler.zig | 53 |
1 files changed, 20 insertions, 33 deletions
diff --git a/src/panic_handler.zig b/src/panic_handler.zig index 84b66d9be..ab8d1cf81 100644 --- a/src/panic_handler.zig +++ b/src/panic_handler.zig @@ -3,36 +3,7 @@ const logger = @import("logger.zig"); const root = @import("root"); usingnamespace @import("global.zig"); -/// This function is used by the Zig language code generation and -/// therefore must be kept in sync with the compiler implementation. -pub fn default_panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn { - @setCold(true); - if (@hasDecl(root, "os") and @hasDecl(root.os, "panic")) { - root.os.panic(msg, error_return_trace); - unreachable; - } - switch (std.builtin.os.tag) { - .freestanding => { - while (true) { - @breakpoint(); - } - }, - .wasi => { - std.debug.warn("{s}", .{msg}); - std.os.abort(); - }, - .uefi => { - // TODO look into using the debug info and logging helpful messages - std.os.abort(); - }, - else => { - const first_trace_addr = @returnAddress(); - std.debug.panicExtra(error_return_trace, first_trace_addr, "{s}", .{msg}); - }, - } -} - -pub fn NewPanicHandler(panic_func: fn handle_panic(msg: []const u8, error_return_type: ?*std.builtin.StackTrace) noreturn) type { +pub fn NewPanicHandler(comptime panic_func: fn handle_panic(msg: []const u8, error_return_type: ?*std.builtin.StackTrace) noreturn) type { return struct { panic_count: usize = 0, skip_next_panic: bool = false, @@ -47,13 +18,29 @@ pub fn NewPanicHandler(panic_func: fn handle_panic(msg: []const u8, error_return }; } pub inline fn handle_panic(msg: []const u8, error_return_type: ?*std.builtin.StackTrace) noreturn { + // This exists to ensure we flush all buffered output before panicking. Output.flush(); - if (@This().Singleton) |singleton| { - singleton.panic_count += 1; + if (msg.len > 0) { + if (Output.isEmojiEnabled()) { + Output.prettyErrorln("<r><red>Bun crashed ðŸ˜ðŸ˜ðŸ˜<r><d>: <r><b>{s}<r>\n", .{msg}); + } else { + Output.prettyErrorln("<r><red>Crash<r><d>:<r> <b>{s}<r>", .{msg}); + } + Output.flush(); + } else { + if (Output.isEmojiEnabled()) { + Output.prettyErrorln("<r><red>Bun will crash now<r> ðŸ˜ðŸ˜ðŸ˜<r>\n", .{}); + Output.flush(); + } else { + Output.printError("Bun has crashed :'(\n", .{}); + } + Output.flush(); } + Output.disableBuffering(); - panic_func(msg, error_return_type); + // // We want to always inline the panic handler so it doesn't show up in the stacktrace. + @call(.{ .modifier = .always_inline }, panic_func, .{ msg, error_return_type }); } }; } |