Much improves the panic function, now prints a stack trace more often. (#86)

Co-authored-by: Felix "xq" Queißner <xq@random-projects.net>
wch-ch32v003
Felix Queißner 2 years ago committed by GitHub
parent 681b3b0d7a
commit 15bc1fc06d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -86,27 +86,27 @@ else
};
/// The microzig default panic handler. Will disable interrupts and loop endlessly.
pub fn microzig_panic(message: []const u8, maybe_stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
pub fn microzig_panic(message: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
// utilize logging functions
std.log.err("microzig PANIC: {s}", .{message});
if (builtin.cpu.arch != .avr) {
var writer = debug.writer();
writer.print("microzig PANIC: {s}\r\n", .{message}) catch unreachable;
if (maybe_stack_trace) |stack_trace| {
var frame_index: usize = 0;
var frames_left: usize = std.math.min(stack_trace.index, stack_trace.instruction_addresses.len);
while (frames_left != 0) : ({
frames_left -= 1;
frame_index = (frame_index + 1) % stack_trace.instruction_addresses.len;
}) {
const return_address = stack_trace.instruction_addresses[frame_index];
writer.print("0x{X:0>8}\r\n", .{return_address}) catch unreachable;
var index: usize = 0;
var iter = std.debug.StackIterator.init(@returnAddress(), null);
while (iter.next()) |address| : (index += 1) {
if (index == 0) {
std.log.err("stack trace:", .{});
}
std.log.err("{d: >3}: 0x{X:0>8}", .{ index, address });
}
}
if (@import("builtin").mode == .Debug) {
// attach a breakpoint, this might trigger another
// panic internally, so only do that in debug mode.
std.log.info("triggering breakpoint...", .{});
@breakpoint();
}
hang();
}

Loading…
Cancel
Save