rcore-tutorial/os/src/lang_items.rs

29 lines
818 B
Rust
Raw Normal View History

2020-11-10 23:02:38 +08:00
use core::panic::PanicInfo;
use core::arch::asm;
2020-11-13 12:06:39 +08:00
use crate::sbi::shutdown;
2021-09-30 10:09:21 -07:00
use crate::task::current_kstack_top;
2020-11-10 23:02:38 +08:00
#[panic_handler]
2020-11-13 12:06:39 +08:00
fn panic(info: &PanicInfo) -> ! {
if let Some(location) = info.location() {
println!("[kernel] Panicked at {}:{} {}", location.file(), location.line(), info.message().unwrap());
2020-11-13 12:06:39 +08:00
} else {
println!("[kernel] Panicked: {}", info.message().unwrap());
2020-11-13 12:06:39 +08:00
}
2021-09-30 10:09:21 -07:00
unsafe { backtrace(); }
2020-11-13 12:06:39 +08:00
shutdown()
2020-11-10 23:02:38 +08:00
}
2021-09-30 10:09:21 -07:00
unsafe fn backtrace() {
let mut fp: usize;
let stop = current_kstack_top();
asm!("mv {}, s0", out(reg) fp);
println!("---START BACKTRACE---");
for i in 0..10 {
if fp == stop { break; }
println!("#{}:ra={:#x}", i, *((fp-8) as *const usize));
fp = *((fp-16) as *const usize);
}
println!("---END BACKTRACE---");
}