fatal.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <cstring>
  6. #include <ctime>
  7. #include <fmt/chrono.h>
  8. #include "common/file_util.h"
  9. #include "common/logging/log.h"
  10. #include "common/scm_rev.h"
  11. #include "common/swap.h"
  12. #include "core/core.h"
  13. #include "core/hle/ipc_helpers.h"
  14. #include "core/hle/kernel/process.h"
  15. #include "core/hle/service/fatal/fatal.h"
  16. #include "core/hle/service/fatal/fatal_p.h"
  17. #include "core/hle/service/fatal/fatal_u.h"
  18. #include "core/reporter.h"
  19. namespace Service::Fatal {
  20. Module::Interface::Interface(std::shared_ptr<Module> module, const char* name)
  21. : ServiceFramework(name), module(std::move(module)) {}
  22. Module::Interface::~Interface() = default;
  23. struct FatalInfo {
  24. enum class Architecture : s32 {
  25. AArch64,
  26. AArch32,
  27. };
  28. const char* ArchAsString() const {
  29. return arch == Architecture::AArch64 ? "AArch64" : "AArch32";
  30. }
  31. std::array<u64_le, 31> registers{};
  32. u64_le sp{};
  33. u64_le pc{};
  34. u64_le pstate{};
  35. u64_le afsr0{};
  36. u64_le afsr1{};
  37. u64_le esr{};
  38. u64_le far{};
  39. std::array<u64_le, 32> backtrace{};
  40. u64_le program_entry_point{};
  41. // Bit flags that indicate which registers have been set with values
  42. // for this context. The service itself uses these to determine which
  43. // registers to specifically print out.
  44. u64_le set_flags{};
  45. u32_le backtrace_size{};
  46. Architecture arch{};
  47. u32_le unk10{}; // TODO(ogniK): Is this even used or is it just padding?
  48. };
  49. static_assert(sizeof(FatalInfo) == 0x250, "FatalInfo is an invalid size");
  50. enum class FatalType : u32 {
  51. ErrorReportAndScreen = 0,
  52. ErrorReport = 1,
  53. ErrorScreen = 2,
  54. };
  55. static void GenerateErrorReport(ResultCode error_code, const FatalInfo& info) {
  56. const auto title_id = Core::CurrentProcess()->GetTitleID();
  57. std::string crash_report = fmt::format(
  58. "Yuzu {}-{} crash report\n"
  59. "Title ID: {:016x}\n"
  60. "Result: 0x{:X} ({:04}-{:04d})\n"
  61. "Set flags: 0x{:16X}\n"
  62. "Program entry point: 0x{:16X}\n"
  63. "\n",
  64. Common::g_scm_branch, Common::g_scm_desc, title_id, error_code.raw,
  65. 2000 + static_cast<u32>(error_code.module.Value()),
  66. static_cast<u32>(error_code.description.Value()), info.set_flags, info.program_entry_point);
  67. if (info.backtrace_size != 0x0) {
  68. crash_report += "Registers:\n";
  69. for (size_t i = 0; i < info.registers.size(); i++) {
  70. crash_report +=
  71. fmt::format(" X[{:02d}]: {:016x}\n", i, info.registers[i]);
  72. }
  73. crash_report += fmt::format(" SP: {:016x}\n", info.sp);
  74. crash_report += fmt::format(" PC: {:016x}\n", info.pc);
  75. crash_report += fmt::format(" PSTATE: {:016x}\n", info.pstate);
  76. crash_report += fmt::format(" AFSR0: {:016x}\n", info.afsr0);
  77. crash_report += fmt::format(" AFSR1: {:016x}\n", info.afsr1);
  78. crash_report += fmt::format(" ESR: {:016x}\n", info.esr);
  79. crash_report += fmt::format(" FAR: {:016x}\n", info.far);
  80. crash_report += "\nBacktrace:\n";
  81. for (size_t i = 0; i < info.backtrace_size; i++) {
  82. crash_report +=
  83. fmt::format(" Backtrace[{:02d}]: {:016x}\n", i, info.backtrace[i]);
  84. }
  85. crash_report += fmt::format("Architecture: {}\n", info.ArchAsString());
  86. crash_report += fmt::format("Unknown 10: 0x{:016x}\n", info.unk10);
  87. }
  88. LOG_ERROR(Service_Fatal, "{}", crash_report);
  89. Core::System::GetInstance().GetReporter().SaveCrashReport(
  90. title_id, error_code, info.set_flags, info.program_entry_point, info.sp, info.pc,
  91. info.pstate, info.afsr0, info.afsr1, info.esr, info.far, info.registers, info.backtrace,
  92. info.backtrace_size, info.ArchAsString(), info.unk10);
  93. }
  94. static void ThrowFatalError(ResultCode error_code, FatalType fatal_type, const FatalInfo& info) {
  95. LOG_ERROR(Service_Fatal, "Threw fatal error type {} with error code 0x{:X}",
  96. static_cast<u32>(fatal_type), error_code.raw);
  97. switch (fatal_type) {
  98. case FatalType::ErrorReportAndScreen:
  99. GenerateErrorReport(error_code, info);
  100. [[fallthrough]];
  101. case FatalType::ErrorScreen:
  102. // Since we have no fatal:u error screen. We should just kill execution instead
  103. ASSERT(false);
  104. break;
  105. // Should not throw a fatal screen but should generate an error report
  106. case FatalType::ErrorReport:
  107. GenerateErrorReport(error_code, info);
  108. break;
  109. }
  110. }
  111. void Module::Interface::ThrowFatal(Kernel::HLERequestContext& ctx) {
  112. LOG_ERROR(Service_Fatal, "called");
  113. IPC::RequestParser rp{ctx};
  114. const auto error_code = rp.Pop<ResultCode>();
  115. ThrowFatalError(error_code, FatalType::ErrorScreen, {});
  116. IPC::ResponseBuilder rb{ctx, 2};
  117. rb.Push(RESULT_SUCCESS);
  118. }
  119. void Module::Interface::ThrowFatalWithPolicy(Kernel::HLERequestContext& ctx) {
  120. LOG_ERROR(Service_Fatal, "called");
  121. IPC::RequestParser rp(ctx);
  122. const auto error_code = rp.Pop<ResultCode>();
  123. const auto fatal_type = rp.PopEnum<FatalType>();
  124. ThrowFatalError(error_code, fatal_type, {}); // No info is passed with ThrowFatalWithPolicy
  125. IPC::ResponseBuilder rb{ctx, 2};
  126. rb.Push(RESULT_SUCCESS);
  127. }
  128. void Module::Interface::ThrowFatalWithCpuContext(Kernel::HLERequestContext& ctx) {
  129. LOG_ERROR(Service_Fatal, "called");
  130. IPC::RequestParser rp(ctx);
  131. const auto error_code = rp.Pop<ResultCode>();
  132. const auto fatal_type = rp.PopEnum<FatalType>();
  133. const auto fatal_info = ctx.ReadBuffer();
  134. FatalInfo info{};
  135. ASSERT_MSG(fatal_info.size() == sizeof(FatalInfo), "Invalid fatal info buffer size!");
  136. std::memcpy(&info, fatal_info.data(), sizeof(FatalInfo));
  137. ThrowFatalError(error_code, fatal_type, info);
  138. IPC::ResponseBuilder rb{ctx, 2};
  139. rb.Push(RESULT_SUCCESS);
  140. }
  141. void InstallInterfaces(SM::ServiceManager& service_manager) {
  142. auto module = std::make_shared<Module>();
  143. std::make_shared<Fatal_P>(module)->InstallAsService(service_manager);
  144. std::make_shared<Fatal_U>(module)->InstallAsService(service_manager);
  145. }
  146. } // namespace Service::Fatal