Commit 6ec9933fd8

Andrew Kelley <superjoe30@gmail.com>
2018-01-15 22:26:13
fix getting debug info twice in default panic handler
1 parent c9ac607
Changed files (2)
std
debug
special
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";
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);
         },