2018-03-20 09:55:20 -04:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-09-23 22:34:11 -04:00
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
#include <ctime>
|
|
|
|
#include <fmt/time.h>
|
|
|
|
#include "common/file_util.h"
|
2018-03-20 09:55:20 -04:00
|
|
|
#include "common/logging/log.h"
|
2018-09-23 22:34:11 -04:00
|
|
|
#include "common/scm_rev.h"
|
|
|
|
#include "common/swap.h"
|
2018-09-25 17:14:19 -04:00
|
|
|
#include "core/core.h"
|
2018-03-20 09:55:20 -04:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
2018-09-23 22:34:11 -04:00
|
|
|
#include "core/hle/kernel/process.h"
|
2018-03-20 09:55:20 -04:00
|
|
|
#include "core/hle/service/fatal/fatal.h"
|
|
|
|
#include "core/hle/service/fatal/fatal_p.h"
|
|
|
|
#include "core/hle/service/fatal/fatal_u.h"
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
namespace Service::Fatal {
|
2018-03-20 09:55:20 -04:00
|
|
|
|
|
|
|
Module::Interface::Interface(std::shared_ptr<Module> module, const char* name)
|
|
|
|
: ServiceFramework(name), module(std::move(module)) {}
|
|
|
|
|
hle/service: Default constructors and destructors in the cpp file where applicable
When a destructor isn't defaulted into a cpp file, it can cause the use
of forward declarations to seemingly fail to compile for non-obvious
reasons. It also allows inlining of the construction/destruction logic
all over the place where a constructor or destructor is invoked, which
can lead to code bloat. This isn't so much a worry here, given the
services won't be created and destroyed frequently.
The cause of the above mentioned non-obvious errors can be demonstrated
as follows:
------- Demonstrative example, if you know how the described error happens, skip forwards -------
Assume we have the following in the header, which we'll call "thing.h":
\#include <memory>
// Forward declaration. For example purposes, assume the definition
// of Object is in some header named "object.h"
class Object;
class Thing {
public:
// assume no constructors or destructors are specified here,
// or the constructors/destructors are defined as:
//
// Thing() = default;
// ~Thing() = default;
//
// ... Some interface member functions would be defined here
private:
std::shared_ptr<Object> obj;
};
If this header is included in a cpp file, (which we'll call "main.cpp"),
this will result in a compilation error, because even though no
destructor is specified, the destructor will still need to be generated by
the compiler because std::shared_ptr's destructor is *not* trivial (in
other words, it does something other than nothing), as std::shared_ptr's
destructor needs to do two things:
1. Decrement the shared reference count of the object being pointed to,
and if the reference count decrements to zero,
2. Free the Object instance's memory (aka deallocate the memory it's
pointing to).
And so the compiler generates the code for the destructor doing this inside main.cpp.
Now, keep in mind, the Object forward declaration is not a complete type. All it
does is tell the compiler "a type named Object exists" and allows us to
use the name in certain situations to avoid a header dependency. So the
compiler needs to generate destruction code for Object, but the compiler
doesn't know *how* to destruct it. A forward declaration doesn't tell
the compiler anything about Object's constructor or destructor. So, the
compiler will issue an error in this case because it's undefined
behavior to try and deallocate (or construct) an incomplete type and
std::shared_ptr and std::unique_ptr make sure this isn't the case
internally.
Now, if we had defaulted the destructor in "thing.cpp", where we also
include "object.h", this would never be an issue, as the destructor
would only have its code generated in one place, and it would be in a
place where the full class definition of Object would be visible to the
compiler.
---------------------- End example ----------------------------
Given these service classes are more than certainly going to change in
the future, this defaults the constructors and destructors into the
relevant cpp files to make the construction and destruction of all of
the services consistent and unlikely to run into cases where forward
declarations are indirectly causing compilation errors. It also has the
plus of avoiding the need to rebuild several services if destruction
logic changes, since it would only be necessary to recompile the single
cpp file.
2018-09-10 21:20:52 -04:00
|
|
|
Module::Interface::~Interface() = default;
|
|
|
|
|
2018-09-23 22:34:11 -04:00
|
|
|
struct FatalInfo {
|
2019-03-30 02:22:21 -04:00
|
|
|
enum class Architecture : s32 {
|
|
|
|
AArch64,
|
|
|
|
AArch32,
|
|
|
|
};
|
|
|
|
|
|
|
|
const char* ArchAsString() const {
|
|
|
|
return arch == Architecture::AArch64 ? "AArch64" : "AArch32";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<u64_le, 31> registers{};
|
|
|
|
u64_le sp{};
|
|
|
|
u64_le pc{};
|
|
|
|
u64_le pstate{};
|
|
|
|
u64_le afsr0{};
|
|
|
|
u64_le afsr1{};
|
|
|
|
u64_le esr{};
|
|
|
|
u64_le far{};
|
2018-09-23 22:34:11 -04:00
|
|
|
|
|
|
|
std::array<u64_le, 32> backtrace{};
|
2019-03-30 02:22:21 -04:00
|
|
|
u64_le program_entry_point{};
|
|
|
|
|
|
|
|
// Bit flags that indicate which registers have been set with values
|
|
|
|
// for this context. The service itself uses these to determine which
|
|
|
|
// registers to specifically print out.
|
|
|
|
u64_le set_flags{};
|
|
|
|
|
2018-09-23 22:34:11 -04:00
|
|
|
u32_le backtrace_size{};
|
2019-03-30 02:22:21 -04:00
|
|
|
Architecture arch{};
|
2018-09-23 22:34:11 -04:00
|
|
|
u32_le unk10{}; // TODO(ogniK): Is this even used or is it just padding?
|
|
|
|
};
|
|
|
|
static_assert(sizeof(FatalInfo) == 0x250, "FatalInfo is an invalid size");
|
|
|
|
|
|
|
|
enum class FatalType : u32 {
|
|
|
|
ErrorReportAndScreen = 0,
|
|
|
|
ErrorReport = 1,
|
|
|
|
ErrorScreen = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void GenerateErrorReport(ResultCode error_code, const FatalInfo& info) {
|
2018-09-29 18:47:00 -04:00
|
|
|
const auto title_id = Core::CurrentProcess()->GetTitleID();
|
2019-03-30 02:22:21 -04:00
|
|
|
std::string crash_report = fmt::format(
|
|
|
|
"Yuzu {}-{} crash report\n"
|
|
|
|
"Title ID: {:016x}\n"
|
|
|
|
"Result: 0x{:X} ({:04}-{:04d})\n"
|
|
|
|
"Set flags: 0x{:16X}\n"
|
|
|
|
"Program entry point: 0x{:16X}\n"
|
|
|
|
"\n",
|
|
|
|
Common::g_scm_branch, Common::g_scm_desc, title_id, error_code.raw,
|
|
|
|
2000 + static_cast<u32>(error_code.module.Value()),
|
|
|
|
static_cast<u32>(error_code.description.Value()), info.set_flags, info.program_entry_point);
|
2018-09-23 22:34:11 -04:00
|
|
|
if (info.backtrace_size != 0x0) {
|
|
|
|
crash_report += "Registers:\n";
|
|
|
|
for (size_t i = 0; i < info.registers.size(); i++) {
|
|
|
|
crash_report +=
|
|
|
|
fmt::format(" X[{:02d}]: {:016x}\n", i, info.registers[i]);
|
|
|
|
}
|
2019-03-30 02:22:21 -04:00
|
|
|
crash_report += fmt::format(" SP: {:016x}\n", info.sp);
|
|
|
|
crash_report += fmt::format(" PC: {:016x}\n", info.pc);
|
|
|
|
crash_report += fmt::format(" PSTATE: {:016x}\n", info.pstate);
|
|
|
|
crash_report += fmt::format(" AFSR0: {:016x}\n", info.afsr0);
|
|
|
|
crash_report += fmt::format(" AFSR1: {:016x}\n", info.afsr1);
|
|
|
|
crash_report += fmt::format(" ESR: {:016x}\n", info.esr);
|
|
|
|
crash_report += fmt::format(" FAR: {:016x}\n", info.far);
|
2018-09-23 22:34:11 -04:00
|
|
|
crash_report += "\nBacktrace:\n";
|
|
|
|
for (size_t i = 0; i < info.backtrace_size; i++) {
|
|
|
|
crash_report +=
|
|
|
|
fmt::format(" Backtrace[{:02d}]: {:016x}\n", i, info.backtrace[i]);
|
|
|
|
}
|
2019-03-30 02:22:21 -04:00
|
|
|
|
|
|
|
crash_report += fmt::format("Architecture: {}\n", info.ArchAsString());
|
2018-09-23 22:34:11 -04:00
|
|
|
crash_report += fmt::format("Unknown 10: 0x{:016x}\n", info.unk10);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_ERROR(Service_Fatal, "{}", crash_report);
|
|
|
|
|
|
|
|
const std::string crashreport_dir =
|
|
|
|
FileUtil::GetUserPath(FileUtil::UserPath::LogDir) + "crash_logs";
|
|
|
|
|
|
|
|
if (!FileUtil::CreateFullPath(crashreport_dir)) {
|
|
|
|
LOG_ERROR(
|
|
|
|
Service_Fatal,
|
|
|
|
"Unable to create crash report directory. Possible log directory permissions issue.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::time_t t = std::time(nullptr);
|
|
|
|
const std::string crashreport_filename =
|
|
|
|
fmt::format("{}/{:016x}-{:%F-%H%M%S}.log", crashreport_dir, title_id, *std::localtime(&t));
|
|
|
|
|
|
|
|
auto file = FileUtil::IOFile(crashreport_filename, "wb");
|
|
|
|
if (file.IsOpen()) {
|
|
|
|
file.WriteString(crash_report);
|
|
|
|
LOG_ERROR(Service_Fatal, "Saving error report to {}", crashreport_filename);
|
|
|
|
} else {
|
|
|
|
LOG_ERROR(Service_Fatal, "Failed to save error report to {}", crashreport_filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ThrowFatalError(ResultCode error_code, FatalType fatal_type, const FatalInfo& info) {
|
2018-12-28 20:55:19 -05:00
|
|
|
LOG_ERROR(Service_Fatal, "Threw fatal error type {} with error code 0x{:X}",
|
|
|
|
static_cast<u32>(fatal_type), error_code.raw);
|
2018-09-23 22:34:11 -04:00
|
|
|
switch (fatal_type) {
|
|
|
|
case FatalType::ErrorReportAndScreen:
|
|
|
|
GenerateErrorReport(error_code, info);
|
|
|
|
[[fallthrough]];
|
|
|
|
case FatalType::ErrorScreen:
|
|
|
|
// Since we have no fatal:u error screen. We should just kill execution instead
|
|
|
|
ASSERT(false);
|
|
|
|
break;
|
|
|
|
// Should not throw a fatal screen but should generate an error report
|
|
|
|
case FatalType::ErrorReport:
|
|
|
|
GenerateErrorReport(error_code, info);
|
|
|
|
break;
|
2019-03-30 03:04:13 -04:00
|
|
|
}
|
2018-09-23 22:34:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::ThrowFatal(Kernel::HLERequestContext& ctx) {
|
|
|
|
LOG_ERROR(Service_Fatal, "called");
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
auto error_code = rp.Pop<ResultCode>();
|
|
|
|
|
|
|
|
ThrowFatalError(error_code, FatalType::ErrorScreen, {});
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:28:30 -04:00
|
|
|
void Module::Interface::ThrowFatalWithPolicy(Kernel::HLERequestContext& ctx) {
|
2018-09-23 22:34:11 -04:00
|
|
|
LOG_ERROR(Service_Fatal, "called");
|
2018-03-20 09:55:20 -04:00
|
|
|
IPC::RequestParser rp(ctx);
|
2018-09-23 22:34:11 -04:00
|
|
|
auto error_code = rp.Pop<ResultCode>();
|
|
|
|
auto fatal_type = rp.PopEnum<FatalType>();
|
|
|
|
|
|
|
|
ThrowFatalError(error_code, fatal_type, {}); // No info is passed with ThrowFatalWithPolicy
|
2018-03-20 09:55:20 -04:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:28:30 -04:00
|
|
|
void Module::Interface::ThrowFatalWithCpuContext(Kernel::HLERequestContext& ctx) {
|
2018-09-23 22:34:11 -04:00
|
|
|
LOG_ERROR(Service_Fatal, "called");
|
|
|
|
IPC::RequestParser rp(ctx);
|
|
|
|
auto error_code = rp.Pop<ResultCode>();
|
|
|
|
auto fatal_type = rp.PopEnum<FatalType>();
|
|
|
|
auto fatal_info = ctx.ReadBuffer();
|
|
|
|
FatalInfo info{};
|
|
|
|
|
|
|
|
ASSERT_MSG(fatal_info.size() == sizeof(FatalInfo), "Invalid fatal info buffer size!");
|
|
|
|
std::memcpy(&info, fatal_info.data(), sizeof(FatalInfo));
|
|
|
|
|
|
|
|
ThrowFatalError(error_code, fatal_type, info);
|
2018-03-20 09:55:20 -04:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallInterfaces(SM::ServiceManager& service_manager) {
|
|
|
|
auto module = std::make_shared<Module>();
|
|
|
|
std::make_shared<Fatal_P>(module)->InstallAsService(service_manager);
|
|
|
|
std::make_shared<Fatal_U>(module)->InstallAsService(service_manager);
|
|
|
|
}
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
} // namespace Service::Fatal
|