From 941dca64dac91a66c3840c218c0d6d427ddd32a0 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Fri, 5 Aug 2016 18:30:38 -0700 Subject: [PATCH] improve panic implementation --- src/lib.rs | 5 ++++- src/panic.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 618bed9..dc225c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,13 @@ -#![feature(lang_items)] +#![feature(alloc, collections, lang_items)] #![no_std] #![crate_type = "rlib"] #![crate_name = "ctru"] extern crate ctru_sys as libctru; +extern crate alloc; +extern crate collections; + pub mod console; pub mod srv; pub mod gfx; diff --git a/src/panic.rs b/src/panic.rs index c0e4d0a..9fb3e82 100644 --- a/src/panic.rs +++ b/src/panic.rs @@ -1,22 +1,65 @@ -use core::fmt::{Arguments, Write}; +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +//! Implementation of various bits and pieces of the `panic!` macro and +//! associated runtime pieces. + +use core::fmt::{self, Display, Write}; +use core::any::Any; + +use collections::String; +use collections::boxed::Box; + +///The compiler wants this to be here. Otherwise it won't be happy. And we like happy compilers. #[lang = "eh_personality"] -extern "C" fn eh_personality() {} +extern fn eh_personality() {} +/// Entry point of panic from the libcore crate. #[lang = "panic_fmt"] -extern fn panic_fmt(fmt: Arguments, file: &str, line: u32) -> ! { +extern fn panic_fmt(msg: fmt::Arguments, file: &'static str, line: u32) -> ! { + begin_panic_fmt(&msg, &(file, line)) +} + +/// The entry point for panicking with a formatted message. +/// +/// This is designed to reduce the amount of code required at the call +/// site as much as possible (so that `panic!()` has as low an impact +/// on (e.g.) the inlining of other functions as possible), by moving +/// the actual formatting into this shared place. +#[inline(never)] +#[cold] +pub fn begin_panic_fmt(msg: &fmt::Arguments, file_line: &(&'static str, u32)) -> ! { + let mut s = String::new(); + let _ = s.write_fmt(*msg); + begin_panic(s, file_line); +} + +/// This is where the main panic logic happens. +#[inline(never)] +#[cold] +pub fn begin_panic(msg: M, file_line: &(&'static str, u32)) -> ! { use gfx::Screen; use console::Console; + let msg = Box::new(msg); + let (file, line) = *file_line; + let mut error_top = Console::init(Screen::Top); let mut error_bottom = Console::init(Screen::Bottom); - writeln!(error_top, "--------------------------------------------------").unwrap(); + write!(error_top, "--------------------------------------------------").unwrap(); writeln!(error_top, "PANIC in {} at line {}:", file, line).unwrap(); - writeln!(error_top, " {}", fmt).unwrap(); + writeln!(error_top, " {}", msg).unwrap(); write!(error_top, "\x1b[29;00H--------------------------------------------------").unwrap(); - writeln!(error_bottom, "").unwrap(); + write!(error_bottom, "").unwrap(); loop {} }