fatal: refactor into sts namespace
This commit is contained in:
@@ -17,133 +17,141 @@
|
||||
#include <cstdio>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <switch.h>
|
||||
#include <atmosphere/version.h>
|
||||
|
||||
#include "fatal_task_error_report.hpp"
|
||||
#include "fatal_config.hpp"
|
||||
#include "fatal_task_error_report.hpp"
|
||||
|
||||
void ErrorReportTask::EnsureReportDirectories() {
|
||||
char path[FS_MAX_PATH];
|
||||
strcpy(path, "sdmc:/atmosphere");
|
||||
mkdir(path, S_IRWXU);
|
||||
strcat(path, "/fatal_reports");
|
||||
mkdir(path, S_IRWXU);
|
||||
strcat(path, "/dumps");
|
||||
mkdir(path, S_IRWXU);
|
||||
}
|
||||
namespace sts::fatal::srv {
|
||||
|
||||
bool ErrorReportTask::GetCurrentTime(u64 *out) {
|
||||
*out = 0;
|
||||
namespace {
|
||||
|
||||
/* Verify that pcv isn't dead. */
|
||||
{
|
||||
bool has_time_service;
|
||||
DoWithSmSession([&]() {
|
||||
Handle dummy;
|
||||
if (R_SUCCEEDED(smRegisterService(&dummy, "time:s", false, 0x20))) {
|
||||
svcCloseHandle(dummy);
|
||||
has_time_service = false;
|
||||
} else {
|
||||
has_time_service = true;
|
||||
}
|
||||
});
|
||||
if (!has_time_service) {
|
||||
return false;
|
||||
/* Helpers. */
|
||||
void TryEnsureReportDirectories() {
|
||||
mkdir("sdmc:/atmosphere", S_IRWXU);
|
||||
mkdir("sdmc:/atmosphere/fatal_reports", S_IRWXU);
|
||||
mkdir("sdmc:/atmosphere/fatal_reports/dumps", S_IRWXU);
|
||||
}
|
||||
}
|
||||
|
||||
/* Try to get the current time. */
|
||||
bool success = true;
|
||||
DoWithSmSession([&]() {
|
||||
success &= R_SUCCEEDED(timeInitialize());
|
||||
});
|
||||
if (success) {
|
||||
success &= R_SUCCEEDED(timeGetCurrentTime(TimeType_LocalSystemClock, out));
|
||||
timeExit();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
bool TryGetCurrentTimestamp(u64 *out) {
|
||||
/* Clear output. */
|
||||
*out = 0;
|
||||
|
||||
void ErrorReportTask::SaveReportToSdCard() {
|
||||
char file_path[FS_MAX_PATH];
|
||||
|
||||
/* Ensure path exists. */
|
||||
EnsureReportDirectories();
|
||||
|
||||
/* Get a timestamp. */
|
||||
u64 timestamp;
|
||||
if (!GetCurrentTime(×tamp)) {
|
||||
timestamp = svcGetSystemTick();
|
||||
}
|
||||
|
||||
/* Open report file. */
|
||||
snprintf(file_path, sizeof(file_path) - 1, "sdmc:/atmosphere/fatal_reports/%011lu_%016lx.log", timestamp, this->title_id);
|
||||
FILE *f_report = fopen(file_path, "w");
|
||||
if (f_report != NULL) {
|
||||
ON_SCOPE_EXIT { fclose(f_report); };
|
||||
|
||||
fprintf(f_report, "Atmosphère Fatal Report (v1.0):\n");
|
||||
fprintf(f_report, "Result: 0x%X (2%03d-%04d)\n\n", this->ctx->error_code, R_MODULE(this->ctx->error_code), R_DESCRIPTION(this->ctx->error_code));
|
||||
fprintf(f_report, "Title ID: %016lx\n", this->title_id);
|
||||
if (strlen(this->ctx->proc_name)) {
|
||||
fprintf(f_report, "Process Name: %s\n", this->ctx->proc_name);
|
||||
}
|
||||
fprintf(f_report, u8"Firmware: %s (Atmosphère %u.%u.%u-%s)\n", GetFatalConfig()->firmware_version.display_version, CURRENT_ATMOSPHERE_VERSION, GetAtmosphereGitRevision());
|
||||
|
||||
if (this->ctx->cpu_ctx.is_aarch32) {
|
||||
fprintf(f_report, "General Purpose Registers:\n");
|
||||
for (size_t i = 0; i < NumAarch32Gprs; i++) {
|
||||
if (this->ctx->has_gprs[i]) {
|
||||
fprintf(f_report, " %3s: %08x\n", Aarch32GprNames[i], this->ctx->cpu_ctx.aarch32_ctx.r[i]);
|
||||
/* Check if we have time service. */
|
||||
{
|
||||
bool has_time_service = false;
|
||||
if (R_FAILED(sm::HasService(&has_time_service, sm::ServiceName::Encode("time:s"))) || !has_time_service) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
fprintf(f_report, " PC: %08x\n", this->ctx->cpu_ctx.aarch32_ctx.pc);
|
||||
fprintf(f_report, "Start Address: %08x\n", this->ctx->cpu_ctx.aarch32_ctx.start_address);
|
||||
fprintf(f_report, "Stack Trace:\n");
|
||||
for (unsigned int i = 0; i < this->ctx->cpu_ctx.aarch32_ctx.stack_trace_size; i++) {
|
||||
fprintf(f_report, " ReturnAddress[%02u]: %08x\n", i, this->ctx->cpu_ctx.aarch32_ctx.stack_trace[i]);
|
||||
|
||||
/* Try to get the current time. */
|
||||
{
|
||||
sm::ScopedServiceHolder<timeInitialize, timeExit> time_holder;
|
||||
return time_holder && R_SUCCEEDED(timeGetCurrentTime(TimeType_LocalSystemClock, out));
|
||||
}
|
||||
} else {
|
||||
fprintf(f_report, "General Purpose Registers:\n");
|
||||
for (size_t i = 0; i < NumAarch64Gprs; i++) {
|
||||
if (this->ctx->has_gprs[i]) {
|
||||
fprintf(f_report, " %3s: %016lx\n", Aarch64GprNames[i], this->ctx->cpu_ctx.aarch64_ctx.x[i]);
|
||||
}
|
||||
|
||||
/* Task definition. */
|
||||
class ErrorReportTask : public ITask {
|
||||
private:
|
||||
void SaveReportToSdCard();
|
||||
public:
|
||||
virtual Result Run() override;
|
||||
virtual const char *GetName() const override {
|
||||
return "WriteErrorReport";
|
||||
}
|
||||
};
|
||||
|
||||
/* Task globals. */
|
||||
ErrorReportTask g_error_report_task;
|
||||
|
||||
/* Task Implementation. */
|
||||
void ErrorReportTask::SaveReportToSdCard() {
|
||||
char file_path[FS_MAX_PATH];
|
||||
|
||||
/* Try to Ensure path exists. */
|
||||
TryEnsureReportDirectories();
|
||||
|
||||
/* Get a timestamp. */
|
||||
u64 timestamp;
|
||||
if (!TryGetCurrentTimestamp(×tamp)) {
|
||||
timestamp = svcGetSystemTick();
|
||||
}
|
||||
|
||||
/* Open report file. */
|
||||
snprintf(file_path, sizeof(file_path) - 1, "sdmc:/atmosphere/fatal_reports/%011lu_%016lx.log", timestamp, static_cast<u64>(this->context->title_id));
|
||||
FILE *f_report = fopen(file_path, "w");
|
||||
if (f_report != NULL) {
|
||||
ON_SCOPE_EXIT { fclose(f_report); };
|
||||
|
||||
fprintf(f_report, "Atmosphère Fatal Report (v1.0):\n");
|
||||
fprintf(f_report, "Result: 0x%X (2%03d-%04d)\n\n", this->context->error_code, R_MODULE(this->context->error_code), R_DESCRIPTION(this->context->error_code));
|
||||
fprintf(f_report, "Title ID: %016lx\n", static_cast<u64>(this->context->title_id));
|
||||
if (strlen(this->context->proc_name)) {
|
||||
fprintf(f_report, "Process Name: %s\n", this->context->proc_name);
|
||||
}
|
||||
fprintf(f_report, u8"Firmware: %s (Atmosphère %u.%u.%u-%s)\n", GetFatalConfig().GetFirmwareVersion().display_version, CURRENT_ATMOSPHERE_VERSION, GetAtmosphereGitRevision());
|
||||
|
||||
if (this->context->cpu_ctx.architecture == CpuContext::Architecture_Aarch32) {
|
||||
fprintf(f_report, "General Purpose Registers:\n");
|
||||
for (size_t i = 0; i <= aarch32::RegisterName_PC; i++) {
|
||||
if (this->context->cpu_ctx.aarch32_ctx.HasRegisterValue(static_cast<aarch32::RegisterName>(i))) {
|
||||
fprintf(f_report, " %3s: %08x\n", aarch32::CpuContext::RegisterNameStrings[i], this->context->cpu_ctx.aarch32_ctx.r[i]);
|
||||
}
|
||||
}
|
||||
fprintf(f_report, "Start Address: %08x\n", this->context->cpu_ctx.aarch32_ctx.base_address);
|
||||
fprintf(f_report, "Stack Trace:\n");
|
||||
for (unsigned int i = 0; i < this->context->cpu_ctx.aarch32_ctx.stack_trace_size; i++) {
|
||||
fprintf(f_report, " ReturnAddress[%02u]: %08x\n", i, this->context->cpu_ctx.aarch32_ctx.stack_trace[i]);
|
||||
}
|
||||
} else {
|
||||
fprintf(f_report, "General Purpose Registers:\n");
|
||||
for (size_t i = 0; i <= aarch64::RegisterName_PC; i++) {
|
||||
if (this->context->cpu_ctx.aarch64_ctx.HasRegisterValue(static_cast<aarch64::RegisterName>(i))) {
|
||||
fprintf(f_report, " %3s: %016lx\n", aarch64::CpuContext::RegisterNameStrings[i], this->context->cpu_ctx.aarch64_ctx.x[i]);
|
||||
}
|
||||
}
|
||||
fprintf(f_report, "Start Address: %016lx\n", this->context->cpu_ctx.aarch64_ctx.base_address);
|
||||
fprintf(f_report, "Stack Trace:\n");
|
||||
for (unsigned int i = 0; i < this->context->cpu_ctx.aarch64_ctx.stack_trace_size; i++) {
|
||||
fprintf(f_report, " ReturnAddress[%02u]: %016lx\n", i, this->context->cpu_ctx.aarch64_ctx.stack_trace[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
fprintf(f_report, " PC: %016lx\n", this->ctx->cpu_ctx.aarch64_ctx.pc);
|
||||
fprintf(f_report, "Start Address: %016lx\n", this->ctx->cpu_ctx.aarch64_ctx.start_address);
|
||||
fprintf(f_report, "Stack Trace:\n");
|
||||
for (unsigned int i = 0; i < this->ctx->cpu_ctx.aarch64_ctx.stack_trace_size; i++) {
|
||||
fprintf(f_report, " ReturnAddress[%02u]: %016lx\n", i, this->ctx->cpu_ctx.aarch64_ctx.stack_trace[i]);
|
||||
|
||||
if (this->context->stack_dump_size) {
|
||||
snprintf(file_path, sizeof(file_path) - 1, "sdmc:/atmosphere/fatal_reports/dumps/%011lu_%016lx.bin", timestamp, static_cast<u64>(this->context->title_id));
|
||||
FILE *f_stackdump = fopen(file_path, "wb");
|
||||
if (f_stackdump == NULL) { return; }
|
||||
ON_SCOPE_EXIT { fclose(f_stackdump); };
|
||||
|
||||
fwrite(this->context->stack_dump, this->context->stack_dump_size, 1, f_stackdump);
|
||||
fflush(f_stackdump);
|
||||
}
|
||||
}
|
||||
|
||||
Result ErrorReportTask::Run() {
|
||||
if (this->context->generate_error_report) {
|
||||
/* Here, Nintendo creates an error report with erpt. AMS will not do that. */
|
||||
}
|
||||
|
||||
/* Save report to SD card. */
|
||||
if (!this->context->is_creport) {
|
||||
this->SaveReportToSdCard();
|
||||
}
|
||||
|
||||
/* Signal we're done with our job. */
|
||||
eventFire(const_cast<Event *>(&this->context->erpt_event));
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (this->ctx->stack_dump_size) {
|
||||
snprintf(file_path, sizeof(file_path) - 1, "sdmc:/atmosphere/fatal_reports/dumps/%011lu_%016lx.bin", timestamp, this->title_id);
|
||||
FILE *f_stackdump = fopen(file_path, "wb");
|
||||
if (f_stackdump == NULL) { return; }
|
||||
ON_SCOPE_EXIT { fclose(f_stackdump); };
|
||||
|
||||
fwrite(this->ctx->stack_dump, this->ctx->stack_dump_size, 1, f_stackdump);
|
||||
ITask *GetErrorReportTask(const ThrowContext *ctx) {
|
||||
g_error_report_task.Initialize(ctx);
|
||||
return &g_error_report_task;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Result ErrorReportTask::Run() {
|
||||
if (this->create_report) {
|
||||
/* Here, Nintendo creates an error report with erpt. AMS will not do that. */
|
||||
}
|
||||
|
||||
/* Save report to SD card. */
|
||||
if (!this->ctx->is_creport) {
|
||||
SaveReportToSdCard();
|
||||
}
|
||||
|
||||
/* Signal we're done with our job. */
|
||||
eventFire(this->erpt_event);
|
||||
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
Reference in New Issue
Block a user