feat: more improvements to panic handling

Follow on panics no longer confuse the panic output and the just in
time debugger now works properly on the local tty.
This commit is contained in:
CJ van den Berg 2025-07-03 16:23:29 +02:00
parent 14dce0a10b
commit e9735d9425
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
5 changed files with 42 additions and 12 deletions

View file

@ -15,8 +15,8 @@
.hash = "dizzy-1.0.0-AAAAAM1wAAAiDbx_6RwcVEOBk8p2XOu8t9WPNc3K7kBK",
},
.thespian = .{
.url = "https://github.com/neurocyte/thespian/archive/829a8d33e92988a51a8c51d204ec766a28c7903d.tar.gz",
.hash = "thespian-0.0.1-owFOjs0TBgAAed7EtHDPtpB7NBn-riNjb7Rkc7a_Voow",
.url = "https://github.com/neurocyte/thespian/archive/b3176b32e62b7615908fa58c99167abbeb58bbc6.tar.gz",
.hash = "thespian-0.0.1-owFOjocZBgBvhsxDpjQoy0vRKrkP1S2HsxSNAnF8LRk2",
},
.themes = .{
.url = "https://github.com/neurocyte/flow-themes/releases/download/master-952f9f630ea9544088fd30293666ee0650b7a690/flow-themes.tar.gz",

View file

@ -149,11 +149,10 @@ pub fn main() anyerror!void {
return list_languages.list(a, stdout.writer(), tty_config);
}
if (builtin.os.tag != .windows)
if (std.posix.getenv("JITDEBUG")) |_|
thespian.install_debugger()
else if (@hasDecl(renderer, "install_crash_handler"))
renderer.install_crash_handler();
if (builtin.os.tag != .windows and @hasDecl(renderer, "install_crash_handler")) {
if (std.posix.getenv("JITDEBUG")) |_| renderer.jit_debugger_enabled = true;
renderer.install_crash_handler();
}
if (args.debug_wait) {
std.debug.print("press return to start", .{});

View file

@ -135,15 +135,46 @@ pub fn install_crash_handler() void {
std.posix.sigaction(std.posix.SIG.ILL, &act, null);
}
pub var jit_debugger_enabled: bool = false;
fn handle_crash(sig: i32, info: *const std.posix.siginfo_t, ctx_ptr: ?*anyopaque) callconv(.c) noreturn {
const debug = @import("std/debug.zig");
debug.lockStdErr();
if (panic_in_progress())
std.posix.abort();
in_panic.store(true, .release);
const cleanup = panic_cleanup;
panic_cleanup = null;
if (cleanup) |self| {
self.vx.deinit(self.allocator, self.tty.anyWriter());
self.tty.deinit();
}
@import("std/debug.zig").handleSegfaultPosix(sig, info, ctx_ptr);
if (jit_debugger_enabled) {
handleSegfaultPosixNoAbort(sig, info, ctx_ptr);
@import("thespian").sighdl_debugger(sig, @ptrCast(@constCast(info)), ctx_ptr);
std.posix.abort();
} else {
debug.handleSegfaultPosix(sig, info, ctx_ptr);
}
unreachable;
}
fn handleSegfaultPosixNoAbort(sig: i32, info: *const std.posix.siginfo_t, ctx_ptr: ?*anyopaque) void {
const debug = @import("std/debug.zig");
debug.resetSegfaultHandler();
const addr = switch (builtin.os.tag) {
.linux => @intFromPtr(info.fields.sigfault.addr),
.freebsd, .macos => @intFromPtr(info.addr),
.netbsd => @intFromPtr(info.info.reason.fault.addr),
.openbsd => @intFromPtr(info.data.fault.addr),
.solaris, .illumos => @intFromPtr(info.reason.fault.addr),
else => unreachable,
};
const code = if (builtin.os.tag == .netbsd) info.info.code else info.code;
debug.dumpSegfaultInfoPosix(sig, code, addr, ctx_ptr);
}
pub fn run(self: *Self) Error!void {

View file

@ -1380,7 +1380,7 @@ pub fn attachSegfaultHandler() void {
updateSegfaultHandler(&act);
}
fn resetSegfaultHandler() void {
pub fn resetSegfaultHandler() void {
if (native_os == .windows) {
if (windows_segfault_handle) |handle| {
assert(windows.kernel32.RemoveVectoredExceptionHandler(handle) != 0);
@ -1438,7 +1438,7 @@ pub fn handleSegfaultPosix(sig: i32, info: *const posix.siginfo_t, ctx_ptr: ?*an
posix.abort();
}
fn dumpSegfaultInfoPosix(sig: i32, code: i32, addr: usize, ctx_ptr: ?*anyopaque) void {
pub fn dumpSegfaultInfoPosix(sig: i32, code: i32, addr: usize, ctx_ptr: ?*anyopaque) void {
const stderr = io.getStdErr().writer();
_ = switch (sig) {
posix.SIG.SEGV => if (native_arch == .x86_64 and native_os == .linux and code == 128) // SI_KERNEL

View file

@ -105,8 +105,8 @@ fn init(allocator: Allocator) InitError!*Self {
var conf, const conf_bufs = root.read_config(@import("config"), allocator);
defer root.free_config(allocator, conf_bufs);
if (conf.start_debugger_on_crash)
tp.install_debugger();
if (@hasDecl(renderer, "install_crash_handler") and conf.start_debugger_on_crash)
renderer.jit_debugger_enabled = true;
const theme_, const parsed_theme = get_theme_by_name(allocator, conf.theme) orelse get_theme_by_name(allocator, "dark_modern") orelse return error.UnknownTheme;
conf.theme = theme_.name;