From 8b8d353e8697efe30c2c813c9a26c950b3bf6b6d Mon Sep 17 00:00:00 2001 From: zhangxinyu <840317537@qq.com> Date: Wed, 17 May 2023 10:32:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E6=80=81?= =?UTF-8?q?=E7=9A=84panic=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch2/user/src/bin/00hello_world.rs | 1 + ch2/user/src/lib.rs | 3 +++ ch2/user/src/user_lang_items.rs | 1 + ch2/user/src/user_lang_items/user_panic.rs | 18 ++++++++++++++++++ 4 files changed, 23 insertions(+) create mode 100644 ch2/user/src/user_lang_items.rs create mode 100644 ch2/user/src/user_lang_items/user_panic.rs diff --git a/ch2/user/src/bin/00hello_world.rs b/ch2/user/src/bin/00hello_world.rs index 77ef2fc..fbf31f5 100644 --- a/ch2/user/src/bin/00hello_world.rs +++ b/ch2/user/src/bin/00hello_world.rs @@ -1,6 +1,7 @@ #![no_std] #![no_main] +use user_lib; #[no_mangle] fn main() { diff --git a/ch2/user/src/lib.rs b/ch2/user/src/lib.rs index 04ead1b..7c21dcb 100644 --- a/ch2/user/src/lib.rs +++ b/ch2/user/src/lib.rs @@ -1,5 +1,8 @@ #![no_std] #![feature(linkage)] // 开启弱链接特性 +#![feature(panic_info_message)] + +mod user_lang_items; #[linkage = "weak"] // 设置我们默认的main函数, 弱链接 #[no_mangle] diff --git a/ch2/user/src/user_lang_items.rs b/ch2/user/src/user_lang_items.rs new file mode 100644 index 0000000..59f155e --- /dev/null +++ b/ch2/user/src/user_lang_items.rs @@ -0,0 +1 @@ +pub mod user_panic; diff --git a/ch2/user/src/user_lang_items/user_panic.rs b/ch2/user/src/user_lang_items/user_panic.rs new file mode 100644 index 0000000..0fd119e --- /dev/null +++ b/ch2/user/src/user_lang_items/user_panic.rs @@ -0,0 +1,18 @@ +use core::panic::PanicInfo; + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + if let Some(location) = info.location() { + // println!( + // "Panicked at {}:{} {}", + // location.file(), + // location.line(), + // info.message().unwrap() + // ); + } else { + // println!("Panicked: {}", info.message().unwrap()); + } + loop { + + } +} \ No newline at end of file