authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-15 16:26:13-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-15 16:26:13-05:00
log6ec9933fd80013104982debab9fbff1463582f19
tree2fb5a768a59e95a94cbeb9ac853643f8777936e5
parentc9ac607bd3bf2cc1cfb941e54e6bcd53f2fcc59d

fix getting debug info twice in default panic handler


2 files changed, 29 insertions(+), 8 deletions(-)

std/debug/index.zig+27-2
...@@ -41,10 +41,21 @@ fn getStderrStream() -> %&io.OutStream {...@@ -41,10 +41,21 @@ fn getStderrStream() -> %&io.OutStream {
41 }41 }
42}42}
4343
44var self_debug_info: ?&ElfStackTrace = null;
45pub fn getSelfDebugInfo() -> %&ElfStackTrace {
46 if (self_debug_info) |info| {
47 return info;
48 } else {
49 const info = try openSelfDebugInfo(global_allocator);
50 self_debug_info = info;
51 return info;
52 }
53}
54
44/// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned.55/// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned.
45pub fn dumpCurrentStackTrace() {56pub fn dumpCurrentStackTrace() {
46 const stderr = getStderrStream() catch return;57 const stderr = getStderrStream() catch return;
47 const debug_info = openSelfDebugInfo(global_allocator) catch |err| {58 const debug_info = getSelfDebugInfo() catch |err| {
48 stderr.print("Unable to open debug info: {}\n", @errorName(err)) catch return;59 stderr.print("Unable to open debug info: {}\n", @errorName(err)) catch return;
49 return;60 return;
50 };61 };
...@@ -58,7 +69,7 @@ pub fn dumpCurrentStackTrace() {...@@ -58,7 +69,7 @@ pub fn dumpCurrentStackTrace() {
58/// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned.69/// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned.
59pub fn dumpStackTrace(stack_trace: &const builtin.StackTrace) {70pub fn dumpStackTrace(stack_trace: &const builtin.StackTrace) {
60 const stderr = getStderrStream() catch return;71 const stderr = getStderrStream() catch return;
61 const debug_info = openSelfDebugInfo(global_allocator) catch |err| {72 const debug_info = getSelfDebugInfo() catch |err| {
62 stderr.print("Unable to open debug info: {}\n", @errorName(err)) catch return;73 stderr.print("Unable to open debug info: {}\n", @errorName(err)) catch return;
63 return;74 return;
64 };75 };
...@@ -119,6 +130,20 @@ pub fn panic(comptime format: []const u8, args: ...) -> noreturn {...@@ -119,6 +130,20 @@ pub fn panic(comptime format: []const u8, args: ...) -> noreturn {
119 os.abort();130 os.abort();
120}131}
121132
133pub fn panicWithTrace(trace: &const builtin.StackTrace, comptime format: []const u8, args: ...) -> noreturn {
134 if (panicking) {
135 os.abort();
136 } else {
137 panicking = true;
138 }
139 const stderr = getStderrStream() catch os.abort();
140 stderr.print(format ++ "\n", args) catch os.abort();
141 dumpStackTrace(trace);
142 dumpCurrentStackTrace();
143
144 os.abort();
145}
146
122const GREEN = "\x1b[32;1m";147const GREEN = "\x1b[32;1m";
123const WHITE = "\x1b[37;1m";148const WHITE = "\x1b[37;1m";
124const DIM = "\x1b[2m";149const DIM = "\x1b[2m";
std/special/panic.zig+2-6
...@@ -13,12 +13,8 @@ pub coldcc fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) -...@@ -13,12 +13,8 @@ pub coldcc fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) -
13 while (true) {}13 while (true) {}
14 },14 },
15 else => {15 else => {
16 if (builtin.have_error_return_tracing) {16 if (error_return_trace) |trace| {
17 if (error_return_trace) |trace| {17 @import("std").debug.panicWithTrace(trace, "{}", msg);
18 std.debug.warn("{}\n", msg);
19 std.debug.dumpStackTrace(trace);
20 @import("std").debug.panic("");
21 }
22 }18 }
23 @import("std").debug.panic("{}", msg);19 @import("std").debug.panic("{}", msg);
24 },20 },