authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-09 22:06:24-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-10 01:38:40-05:00
log60b2031831320186f3920d63cfa35bda40930450
tree1c09db35eab1d6bac66db99f02a891811da230c3
parent20011a7a1c1f08644cd82a3c3e1d57cba9980695

improvements to stack traces

* @panic generates an error return trace * printing an error return trace no longer interferes with normal stack traces. * instead of ignore_frame_count, we look at the return address when you call panic, and that's the first stack trace function makes stack traces much cleaner - the error return trace flows gracefully into the stack trace

3 files changed, 34 insertions(+), 14 deletions(-)

src/codegen.cpp+2-2
......@@ -3299,8 +3299,8 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
32993299static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *executable,
33003300 IrInstructionErrorReturnTrace *instruction)
33013301{
3302 TypeTableEntry *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
33033302 if (g->cur_err_ret_trace_val == nullptr) {
3303 TypeTableEntry *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
33043304 return LLVMConstNull(ptr_to_stack_trace_type->type_ref);
33053305 }
33063306 return g->cur_err_ret_trace_val;
......@@ -3925,7 +3925,7 @@ static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *exec
39253925}
39263926
39273927static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInstructionPanic *instruction) {
3928 gen_panic(g, ir_llvm_value(g, instruction->msg), nullptr);
3928 gen_panic(g, ir_llvm_value(g, instruction->msg), g->cur_err_ret_trace_val);
39293929 return nullptr;
39303930}
39313931
std/debug/index.zig+29-10
......@@ -9,6 +9,7 @@ const macho = std.macho;
99const ArrayList = std.ArrayList;
1010const builtin = @import("builtin");
1111
12pub var stack_trace_start_address: ?usize = null;
1213pub const FailingAllocator = @import("failing_allocator.zig").FailingAllocator;
1314
1415/// Tries to write to stderr, unbuffered, and ignores any error returned.
......@@ -51,8 +52,7 @@ pub fn dumpCurrentStackTrace() void {
5152 stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return;
5253 return;
5354 };
54 defer debug_info.close();
55 writeCurrentStackTrace(stderr, global_allocator, debug_info, stderr_file.isTty(), 1) catch |err| {
55 writeCurrentStackTrace(stderr, global_allocator, debug_info, stderr_file.isTty()) catch |err| {
5656 stderr.print("Unable to dump stack trace: {}\n", @errorName(err)) catch return;
5757 return;
5858 };
......@@ -65,7 +65,6 @@ pub fn dumpStackTrace(stack_trace: &const builtin.StackTrace) void {
6565 stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return;
6666 return;
6767 };
68 defer debug_info.close();
6968 writeStackTrace(stack_trace, stderr, global_allocator, debug_info, stderr_file.isTty()) catch |err| {
7069 stderr.print("Unable to dump stack trace: {}\n", @errorName(err)) catch return;
7170 return;
......@@ -162,18 +161,38 @@ pub fn writeStackTrace(stack_trace: &const builtin.StackTrace, out_stream: var,
162161}
163162
164163pub fn writeCurrentStackTrace(out_stream: var, allocator: &mem.Allocator,
165 debug_info: &ElfStackTrace, tty_color: bool, ignore_frame_count: usize) !void
164 debug_info: &ElfStackTrace, tty_color: bool) !void
166165{
167 var ignored_count: usize = 0;
166 const AddressState = union(enum) {
167 NotLookingForStartAddress,
168 LookingForStartAddress: usize,
169 FoundStartAddress,
170 };
171 // TODO: I want to express like this:
172 //var addr_state = if (stack_trace_start_address) |addr| AddressState { .LookingForStartAddress = addr }
173 // else AddressState.NotLookingForStartAddress;
174 var addr_state: AddressState = undefined;
175 if (stack_trace_start_address) |addr| {
176 addr_state = AddressState { .LookingForStartAddress = addr };
177 } else {
178 addr_state = AddressState.NotLookingForStartAddress;
179 }
168180
169181 var fp = @ptrToInt(@frameAddress());
170182 while (fp != 0) : (fp = *@intToPtr(&const usize, fp)) {
171 if (ignored_count < ignore_frame_count) {
172 ignored_count += 1;
173 continue;
174 }
175
176183 const return_address = *@intToPtr(&const usize, fp + @sizeOf(usize));
184
185 switch (addr_state) {
186 AddressState.NotLookingForStartAddress => continue,
187 AddressState.LookingForStartAddress => |addr| {
188 if (return_address == addr) {
189 addr_state = AddressState.FoundStartAddress;
190 } else {
191 continue;
192 }
193 },
194 AddressState.FoundStartAddress => {},
195 }
177196 try printSourceAtAddress(debug_info, out_stream, return_address);
178197 }
179198}
std/special/panic.zig+3-2
......@@ -14,10 +14,11 @@ pub fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) noreturn
1414 while (true) {}
1515 },
1616 else => {
17 std.debug.stack_trace_start_address = @ptrToInt(@returnAddress());
1718 if (error_return_trace) |trace| {
18 @import("std").debug.panicWithTrace(trace, "{}", msg);
19 std.debug.panicWithTrace(trace, "{}", msg);
1920 }
20 @import("std").debug.panic("{}", msg);
21 std.debug.panic("{}", msg);
2122 },
2223 }
2324}