panic_handler.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use crate::print::*;
  2. use alloc::boxed::Box;
  3. use core::any::Any;
  4. use core::cell::Cell;
  5. use core::panic::{Location, PanicInfo};
  6. #[thread_local]
  7. static PANIC_COUNT: Cell<usize> = Cell::new(0);
  8. #[link(name = "c")]
  9. extern "C" {}
  10. pub(crate) fn drop_panic() {
  11. eprintln!("Rust panics must be rethrown");
  12. }
  13. pub(crate) fn foreign_exception() {
  14. eprintln!("Rust cannot catch foreign exceptions");
  15. }
  16. pub(crate) fn panic_caught() {
  17. PANIC_COUNT.set(0);
  18. }
  19. fn do_panic(msg: Box<dyn Any + Send>) -> ! {
  20. if PANIC_COUNT.get() >= 1 {
  21. eprintln!("thread panicked while processing panic. aborting.");
  22. core::intrinsics::abort();
  23. }
  24. PANIC_COUNT.set(1);
  25. let code = crate::panic::begin_panic(Box::new(msg));
  26. eprintln!("failed to initiate panic, error {}", code.0);
  27. core::intrinsics::abort();
  28. }
  29. #[panic_handler]
  30. fn panic(info: &PanicInfo<'_>) -> ! {
  31. eprintln!("{}", info);
  32. struct NoPayload;
  33. do_panic(Box::new(NoPayload))
  34. }
  35. #[track_caller]
  36. pub fn panic_any<M: 'static + Any + Send>(msg: M) -> ! {
  37. eprintln!("panicked at {}", Location::caller());
  38. do_panic(Box::new(msg))
  39. }