From 6ec9933fd80013104982debab9fbff1463582f19 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 15 Jan 2018 16:26:13 -0500 Subject: [PATCH] fix getting debug info twice in default panic handler --- std/debug/index.zig | 29 +++++++++++++++++++++++++++-- std/special/panic.zig | 8 ++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/std/debug/index.zig b/std/debug/index.zig index 5dc7b0dbfa..87e81ab8c2 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -41,10 +41,21 @@ fn getStderrStream() -> %&io.OutStream { } } +var self_debug_info: ?&ElfStackTrace = null; +pub fn getSelfDebugInfo() -> %&ElfStackTrace { + if (self_debug_info) |info| { + return info; + } else { + const info = try openSelfDebugInfo(global_allocator); + self_debug_info = info; + return info; + } +} + /// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned. pub fn dumpCurrentStackTrace() { const stderr = getStderrStream() catch return; - const debug_info = openSelfDebugInfo(global_allocator) catch |err| { + const debug_info = getSelfDebugInfo() catch |err| { stderr.print("Unable to open debug info: {}\n", @errorName(err)) catch return; return; }; @@ -58,7 +69,7 @@ pub fn dumpCurrentStackTrace() { /// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned. pub fn dumpStackTrace(stack_trace: &const builtin.StackTrace) { const stderr = getStderrStream() catch return; - const debug_info = openSelfDebugInfo(global_allocator) catch |err| { + const debug_info = getSelfDebugInfo() catch |err| { stderr.print("Unable to open debug info: {}\n", @errorName(err)) catch return; return; }; @@ -119,6 +130,20 @@ pub fn panic(comptime format: []const u8, args: ...) -> noreturn { os.abort(); } +pub fn panicWithTrace(trace: &const builtin.StackTrace, comptime format: []const u8, args: ...) -> noreturn { + if (panicking) { + os.abort(); + } else { + panicking = true; + } + const stderr = getStderrStream() catch os.abort(); + stderr.print(format ++ "\n", args) catch os.abort(); + dumpStackTrace(trace); + dumpCurrentStackTrace(); + + os.abort(); +} + const GREEN = "\x1b[32;1m"; const WHITE = "\x1b[37;1m"; const DIM = "\x1b[2m"; diff --git a/std/special/panic.zig b/std/special/panic.zig index d26166fdac..1b22658c7f 100644 --- a/std/special/panic.zig +++ b/std/special/panic.zig @@ -13,12 +13,8 @@ pub coldcc fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) - while (true) {} }, else => { - if (builtin.have_error_return_tracing) { - if (error_return_trace) |trace| { - std.debug.warn("{}\n", msg); - std.debug.dumpStackTrace(trace); - @import("std").debug.panic(""); - } + if (error_return_trace) |trace| { + @import("std").debug.panicWithTrace(trace, "{}", msg); } @import("std").debug.panic("{}", msg); },