rcore-tutorial/os/src/lang_items.rs

38 lines
916 B
Rust
Raw Normal View History

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;
2022-01-22 12:40:54 -08:00
use core::arch::asm;
use core::panic::PanicInfo;
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() {
2022-01-22 12:40:54 -08:00
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
}
2022-01-22 12:40:54 -08:00
unsafe {
backtrace();
}
shutdown(true)
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 {
2022-01-22 12:40:54 -08:00
if fp == stop {
break;
}
println!("#{}:ra={:#x}", i, *((fp - 8) as *const usize));
fp = *((fp - 16) as *const usize);
2021-09-30 10:09:21 -07:00
}
println!("---END BACKTRACE---");
}