CrashHandler.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Arch/CPU.h>
  7. #include <Kernel/Arch/RegisterState.h>
  8. #include <Kernel/Panic.h>
  9. #include <Kernel/Tasks/Process.h>
  10. #include <Kernel/Tasks/Thread.h>
  11. namespace Kernel {
  12. void handle_crash(Kernel::RegisterState const& regs, char const* description, int signal, bool out_of_memory)
  13. {
  14. auto* current_thread = Thread::current();
  15. if (!current_thread)
  16. PANIC("{} with !Thread::current()", description);
  17. auto crashed_in_kernel = regs.previous_mode() == ExecutionMode::Kernel;
  18. if (!crashed_in_kernel && current_thread->has_signal_handler(signal) && !current_thread->should_ignore_signal(signal) && !current_thread->is_signal_masked(signal)) {
  19. current_thread->send_urgent_signal_to_self(signal);
  20. return;
  21. }
  22. auto& process = current_thread->process();
  23. // If a process crashed while inspecting another process,
  24. // make sure we switch back to the right page tables.
  25. Memory::MemoryManager::enter_process_address_space(process);
  26. dmesgln("CRASH: CPU #{} {} in {}", Processor::current_id(), description, regs.previous_mode() == ExecutionMode::Kernel ? "kernel"sv : "userspace"sv);
  27. dump_registers(regs);
  28. if (crashed_in_kernel) {
  29. process.address_space().with([&](auto& space) { space->dump_regions(); });
  30. PANIC("Crash in kernel");
  31. }
  32. process.crash(signal, { regs }, out_of_memory);
  33. }
  34. }