aboutsummaryrefslogtreecommitdiff
path: root/src/panic_handler.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-05-01 01:28:40 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-05-01 01:28:40 -0700
commit3050d5a1a4def6fe90b62854e2252e308f5f708a (patch)
tree508eb5c8033779b92650f6d129251c1b9a5199b8 /src/panic_handler.zig
parent7005f4c43c8fa403b85f14afcf4c9b5a4c8c4cbc (diff)
downloadbun-3050d5a1a4def6fe90b62854e2252e308f5f708a.tar.gz
bun-3050d5a1a4def6fe90b62854e2252e308f5f708a.tar.zst
bun-3050d5a1a4def6fe90b62854e2252e308f5f708a.zip
Assorted bugfixes but the next step really is porting tests and fixing
Former-commit-id: f59ec8d6c0c95217c1dc24e2ed2dc49aed122a68
Diffstat (limited to 'src/panic_handler.zig')
-rw-r--r--src/panic_handler.zig58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/panic_handler.zig b/src/panic_handler.zig
new file mode 100644
index 000000000..475d55514
--- /dev/null
+++ b/src/panic_handler.zig
@@ -0,0 +1,58 @@
+const std = @import("std");
+const logger = @import("logger.zig");
+const root = @import("root");
+
+const USERLAND_PANIC_MESSAGE = "iNtErNaL sErVeR eRrOr";
+
+/// 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 {
+ return struct {
+ panic_count: usize = 0,
+ skip_next_panic: bool = false,
+ log: *logger.Log,
+
+ pub var Singleton: ?*Handler = null;
+ const Handler = @This();
+
+ pub fn init(log: *logger.Log) Handler {
+ return Handler{
+ .log = log,
+ };
+ }
+ pub fn handle_panic(msg: []const u8, error_return_type: ?*std.builtin.StackTrace) callconv(.Inline) noreturn {
+ if (@This().Singleton) |singleton| {
+ singleton.panic_count += 1;
+ }
+
+ panic_func(msg, error_return_type);
+ }
+ };
+}