Atmosphere/stratosphere/fatal/source/fatal_task_error_report.cpp

179 lines
9.2 KiB
C++
Raw Normal View History

2018-11-10 09:04:40 +00:00
/*
* Copyright (c) Atmosphère-NX
2018-11-10 09:04:40 +00:00
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "fatal_config.hpp"
2019-07-19 02:09:35 +00:00
#include "fatal_task_error_report.hpp"
2020-03-08 11:50:47 +00:00
#include "fatal_scoped_file.hpp"
namespace ams::fatal::srv {
2019-07-19 02:09:35 +00:00
namespace {
/* Helpers. */
void TryEnsureReportDirectories() {
2020-03-08 11:50:47 +00:00
fs::EnsureDirectoryRecursively("sdmc:/atmosphere/fatal_reports/dumps");
2019-07-19 02:09:35 +00:00
}
bool TryGetCurrentTimestamp(u64 *out) {
/* Clear output. */
*out = 0;
/* Check if we have time service. */
{
bool has_time_service = false;
if (R_FAILED(sm::HasService(std::addressof(has_time_service), sm::ServiceName::Encode("time:s"))) || !has_time_service) {
2019-07-19 02:09:35 +00:00
return false;
}
}
2019-07-19 02:09:35 +00:00
/* Try to get the current time. */
{
if (R_FAILED(::timeInitialize())) {
return false;
}
ON_SCOPE_EXIT { ::timeExit(); };
return R_SUCCEEDED(::timeGetCurrentTime(TimeType_LocalSystemClock, out));
}
}
2019-06-17 23:41:03 +00:00
2019-07-19 02:09:35 +00:00
/* Task definition. */
class ErrorReportTask : public ITaskWithDefaultStack {
2019-07-19 02:09:35 +00:00
private:
void SaveReportToSdCard();
public:
virtual Result Run() override;
virtual const char *GetName() const override {
return "WriteErrorReport";
}
};
2019-07-19 02:09:35 +00:00
/* Task globals. */
ErrorReportTask g_error_report_task;
2019-06-17 23:41:03 +00:00
2019-07-19 02:09:35 +00:00
/* Task Implementation. */
void ErrorReportTask::SaveReportToSdCard() {
2020-03-08 11:50:47 +00:00
char file_path[fs::EntryNameLengthMax + 1];
2019-06-17 23:41:03 +00:00
2019-07-19 02:09:35 +00:00
/* Try to Ensure path exists. */
TryEnsureReportDirectories();
2019-06-17 23:41:03 +00:00
2019-07-19 02:09:35 +00:00
/* Get a timestamp. */
u64 timestamp;
if (!TryGetCurrentTimestamp(std::addressof(timestamp))) {
2021-10-04 21:54:13 +00:00
timestamp = os::GetSystemTick().GetInt64Value();
2019-07-19 02:09:35 +00:00
}
2019-06-17 23:41:03 +00:00
2019-07-19 02:09:35 +00:00
/* Open report file. */
2020-03-08 11:50:47 +00:00
{
2021-10-10 07:14:06 +00:00
util::SNPrintf(file_path, sizeof(file_path) - 1, "sdmc:/atmosphere/fatal_reports/%011lu_%016lx.log", timestamp, static_cast<u64>(m_context->program_id));
2020-03-08 11:50:47 +00:00
ScopedFile file(file_path);
if (file.IsOpen()) {
file.WriteFormat("Atmosphère Fatal Report (v1.1):\n");
2021-10-10 07:14:06 +00:00
file.WriteFormat("Result: 0x%X (2%03d-%04d)\n\n", m_context->result.GetValue(), m_context->result.GetModule(), m_context->result.GetDescription());
file.WriteFormat("Program ID: %016lx\n", static_cast<u64>(m_context->program_id));
if (strlen(m_context->proc_name)) {
file.WriteFormat("Process Name: %s\n", m_context->proc_name);
2019-07-19 02:09:35 +00:00
}
file.WriteFormat("Firmware: %s (Atmosphère %u.%u.%u-%s)\n", GetFatalConfig().GetFirmwareVersion().display_version, ATMOSPHERE_RELEASE_VERSION, ams::GetGitRevision());
2020-03-08 11:50:47 +00:00
2021-10-10 07:14:06 +00:00
if (m_context->cpu_ctx.architecture == CpuContext::Architecture_Aarch32) {
2020-03-08 11:50:47 +00:00
file.WriteFormat("General Purpose Registers:\n");
for (size_t i = 0; i <= aarch32::RegisterName_PC; i++) {
2021-10-10 07:14:06 +00:00
if (m_context->cpu_ctx.aarch32_ctx.HasRegisterValue(static_cast<aarch32::RegisterName>(i))) {
file.WriteFormat( " %3s: %08x\n", aarch32::CpuContext::RegisterNameStrings[i], m_context->cpu_ctx.aarch32_ctx.r[i]);
2020-03-08 11:50:47 +00:00
}
}
2021-10-10 07:14:06 +00:00
file.WriteFormat("Start Address: %08x\n", m_context->cpu_ctx.aarch32_ctx.base_address);
2020-03-08 11:50:47 +00:00
file.WriteFormat("Stack Trace:\n");
2021-10-10 07:14:06 +00:00
for (unsigned int i = 0; i < m_context->cpu_ctx.aarch32_ctx.stack_trace_size; i++) {
file.WriteFormat(" ReturnAddress[%02u]: %08x\n", i, m_context->cpu_ctx.aarch32_ctx.stack_trace[i]);
2020-03-08 11:50:47 +00:00
}
} else {
file.WriteFormat("General Purpose Registers:\n");
for (size_t i = 0; i <= aarch64::RegisterName_PC; i++) {
2021-10-10 07:14:06 +00:00
if (m_context->cpu_ctx.aarch64_ctx.HasRegisterValue(static_cast<aarch64::RegisterName>(i))) {
file.WriteFormat( " %3s: %016lx\n", aarch64::CpuContext::RegisterNameStrings[i], m_context->cpu_ctx.aarch64_ctx.x[i]);
2020-03-08 11:50:47 +00:00
}
}
2021-10-10 07:14:06 +00:00
file.WriteFormat("Start Address: %016lx\n", m_context->cpu_ctx.aarch64_ctx.base_address);
2020-03-08 11:50:47 +00:00
file.WriteFormat("Stack Trace:\n");
2021-10-10 07:14:06 +00:00
for (unsigned int i = 0; i < m_context->cpu_ctx.aarch64_ctx.stack_trace_size; i++) {
file.WriteFormat(" ReturnAddress[%02u]: %016lx\n", i, m_context->cpu_ctx.aarch64_ctx.stack_trace[i]);
2019-07-19 02:09:35 +00:00
}
}
2019-12-31 23:23:25 +00:00
2021-10-10 07:14:06 +00:00
if (m_context->stack_dump_size != 0) {
2020-03-08 11:50:47 +00:00
file.WriteFormat("Stack Dump: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
for (size_t i = 0; i < 0x10; i++) {
const size_t ofs = i * 0x10;
file.WriteFormat(" %012lx %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2021-10-10 07:14:06 +00:00
m_context->stack_dump_base + ofs, m_context->stack_dump[ofs + 0], m_context->stack_dump[ofs + 1], m_context->stack_dump[ofs + 2], m_context->stack_dump[ofs + 3], m_context->stack_dump[ofs + 4], m_context->stack_dump[ofs + 5], m_context->stack_dump[ofs + 6], m_context->stack_dump[ofs + 7],
m_context->stack_dump[ofs + 8], m_context->stack_dump[ofs + 9], m_context->stack_dump[ofs + 10], m_context->stack_dump[ofs + 11], m_context->stack_dump[ofs + 12], m_context->stack_dump[ofs + 13], m_context->stack_dump[ofs + 14], m_context->stack_dump[ofs + 15]);
2020-03-08 11:50:47 +00:00
}
2019-12-31 23:23:25 +00:00
}
2021-10-10 07:14:06 +00:00
if (m_context->tls_address != 0) {
file.WriteFormat("TLS Address: %016lx\n", m_context->tls_address);
2020-03-08 11:50:47 +00:00
file.WriteFormat("TLS Dump: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
for (size_t i = 0; i < 0x10; i++) {
const size_t ofs = i * 0x10;
file.WriteFormat(" %012lx %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2021-10-10 07:14:06 +00:00
m_context->tls_address + ofs, m_context->tls_dump[ofs + 0], m_context->tls_dump[ofs + 1], m_context->tls_dump[ofs + 2], m_context->tls_dump[ofs + 3], m_context->tls_dump[ofs + 4], m_context->tls_dump[ofs + 5], m_context->tls_dump[ofs + 6], m_context->tls_dump[ofs + 7],
m_context->tls_dump[ofs + 8], m_context->tls_dump[ofs + 9], m_context->tls_dump[ofs + 10], m_context->tls_dump[ofs + 11], m_context->tls_dump[ofs + 12], m_context->tls_dump[ofs + 13], m_context->tls_dump[ofs + 14], m_context->tls_dump[ofs + 15]);
2020-03-08 11:50:47 +00:00
}
2019-12-31 23:23:25 +00:00
}
}
}
2019-07-19 02:09:35 +00:00
2019-12-31 23:23:25 +00:00
/* Dump data to file. */
{
2021-10-10 07:14:06 +00:00
util::SNPrintf(file_path, sizeof(file_path) - 1, "sdmc:/atmosphere/fatal_reports/dumps/%011lu_%016lx.bin", timestamp, static_cast<u64>(m_context->program_id));
2020-03-08 11:50:47 +00:00
ScopedFile file(file_path);
if (file.IsOpen()) {
2021-10-10 07:14:06 +00:00
file.Write(m_context->tls_dump, sizeof(m_context->tls_dump));
if (m_context->stack_dump_size) {
file.Write(m_context->stack_dump, m_context->stack_dump_size);
2020-03-08 11:50:47 +00:00
}
2019-12-31 23:23:25 +00:00
}
}
}
2019-06-17 23:41:03 +00:00
2019-07-19 02:09:35 +00:00
Result ErrorReportTask::Run() {
2021-10-10 07:14:06 +00:00
if (m_context->generate_error_report) {
2019-07-19 02:09:35 +00:00
/* Here, Nintendo creates an error report with erpt. AMS will not do that. */
}
2019-06-17 23:41:03 +00:00
2019-07-19 02:09:35 +00:00
/* Save report to SD card. */
2021-10-10 07:14:06 +00:00
if (!m_context->is_creport) {
2019-07-19 02:09:35 +00:00
this->SaveReportToSdCard();
}
2019-06-17 23:41:03 +00:00
2019-07-19 02:09:35 +00:00
/* Signal we're done with our job. */
2021-10-10 07:14:06 +00:00
m_context->erpt_event->Signal();
2018-11-10 09:04:40 +00:00
return ResultSuccess();
2019-07-19 02:09:35 +00:00
}
2019-06-17 23:41:03 +00:00
2018-11-10 09:04:40 +00:00
}
2019-06-17 23:41:03 +00:00
2019-07-19 02:09:35 +00:00
ITask *GetErrorReportTask(const ThrowContext *ctx) {
g_error_report_task.Initialize(ctx);
return std::addressof(g_error_report_task);
2019-07-19 02:09:35 +00:00
}
2019-06-17 23:41:03 +00:00
2019-07-19 02:09:35 +00:00
}