fatal: use fs bindings

This commit is contained in:
Michael Scire 2020-03-08 04:50:47 -07:00
parent 2181adb82b
commit f7fb689412
4 changed files with 211 additions and 65 deletions

View file

@ -96,7 +96,7 @@ void __appInit(void) {
R_ABORT_UNLESS(fsInitialize());
});
R_ABORT_UNLESS(fsdevMountSdmc());
R_ABORT_UNLESS(fs::MountSdCard("sdmc"));
/* fatal cannot throw fatal, so don't do: ams::CheckApiVersion(); */
}

View file

@ -0,0 +1,97 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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 "fatal_scoped_file.hpp"
namespace ams::fatal::srv {
namespace {
/* Convenience definitions. */
constexpr size_t MaximumLineLength = 0x20;
os::Mutex g_format_lock;
char g_format_buffer[2 * os::MemoryPageSize];
}
void ScopedFile::WriteString(const char *str) {
this->Write(str, std::strlen(str));
}
void ScopedFile::WriteFormat(const char *fmt, ...) {
/* Acquire exclusive access to the format buffer. */
std::scoped_lock lk(g_format_lock);
/* Format to the buffer. */
{
std::va_list vl;
va_start(vl, fmt);
std::vsnprintf(g_format_buffer, sizeof(g_format_buffer), fmt, vl);
va_end(vl);
}
/* Write data. */
this->WriteString(g_format_buffer);
}
void ScopedFile::DumpMemory(const char *prefix, const void *data, size_t size) {
const u8 *data_u8 = reinterpret_cast<const u8 *>(data);
const int prefix_len = std::strlen(prefix);
size_t data_ofs = 0;
size_t remaining = size;
bool first = true;
while (remaining) {
const size_t cur_size = std::min(MaximumLineLength, remaining);
/* Print the line prefix. */
if (first) {
this->WriteFormat("%s", prefix);
first = false;
} else {
this->WriteFormat("%*s", prefix_len, "");
}
/* Dump up to 0x20 of hex memory. */
{
char hex[MaximumLineLength * 2 + 2] = {};
for (size_t i = 0; i < cur_size; i++) {
std::snprintf(hex + i * 2, 3, "%02X", data_u8[data_ofs++]);
}
hex[cur_size * 2 + 0] = '\n';
hex[cur_size * 2 + 1] = '\x00';
this->WriteString(hex);
}
/* Continue. */
remaining -= cur_size;
}
}
void ScopedFile::Write(const void *data, size_t size) {
/* If we're not open, we can't write. */
if (!this->IsOpen()) {
return;
}
/* Advance, if we write successfully. */
if (R_SUCCEEDED(fs::WriteFile(this->file, this->offset, data, size, fs::WriteOption::Flush))) {
this->offset += size;
}
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::fatal::srv {
class ScopedFile {
NON_COPYABLE(ScopedFile);
NON_MOVEABLE(ScopedFile);
private:
fs::FileHandle file;
s64 offset;
bool opened;
public:
ScopedFile(const char *path) : file(), offset(), opened(false) {
if (R_SUCCEEDED(fs::CreateFile(path, 0))) {
this->opened = R_SUCCEEDED(fs::OpenFile(std::addressof(this->file), path, fs::OpenMode_Write | fs::OpenMode_Append));
}
}
~ScopedFile() {
if (this->opened) {
fs::CloseFile(file);
}
}
bool IsOpen() const {
return this->opened;
}
void WriteString(const char *str);
void WriteFormat(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
void DumpMemory(const char *prefix, const void *data, size_t size);
void Write(const void *data, size_t size);
};
}

View file

@ -17,6 +17,7 @@
#include <sys/types.h>
#include "fatal_config.hpp"
#include "fatal_task_error_report.hpp"
#include "fatal_scoped_file.hpp"
namespace ams::fatal::srv {
@ -24,9 +25,7 @@ namespace ams::fatal::srv {
/* Helpers. */
void TryEnsureReportDirectories() {
mkdir("sdmc:/atmosphere", S_IRWXU);
mkdir("sdmc:/atmosphere/fatal_reports", S_IRWXU);
mkdir("sdmc:/atmosphere/fatal_reports/dumps", S_IRWXU);
fs::EnsureDirectoryRecursively("sdmc:/atmosphere/fatal_reports/dumps");
}
bool TryGetCurrentTimestamp(u64 *out) {
@ -64,7 +63,7 @@ namespace ams::fatal::srv {
/* Task Implementation. */
void ErrorReportTask::SaveReportToSdCard() {
char file_path[FS_MAX_PATH];
char file_path[fs::EntryNameLengthMax + 1];
/* Try to Ensure path exists. */
TryEnsureReportDirectories();
@ -76,79 +75,77 @@ namespace ams::fatal::srv {
}
/* Open report file. */
snprintf(file_path, sizeof(file_path) - 1, "sdmc:/atmosphere/fatal_reports/%011lu_%016lx.log", timestamp, static_cast<u64>(this->context->program_id));
FILE *f_report = fopen(file_path, "w");
if (f_report != NULL) {
ON_SCOPE_EXIT { fclose(f_report); };
{
std::snprintf(file_path, sizeof(file_path) - 1, "sdmc:/atmosphere/fatal_reports/%011lu_%016lx.log", timestamp, static_cast<u64>(this->context->program_id));
ScopedFile file(file_path);
if (file.IsOpen()) {
file.WriteFormat(u8"Atmosphère Fatal Report (v1.1):\n");
file.WriteFormat("Result: 0x%X (2%03d-%04d)\n\n", this->context->result.GetValue(), this->context->result.GetModule(), this->context->result.GetDescription());
file.WriteFormat("Program ID: %016lx\n", static_cast<u64>(this->context->program_id));
if (strlen(this->context->proc_name)) {
file.WriteFormat("Process Name: %s\n", this->context->proc_name);
}
file.WriteFormat(u8"Firmware: %s (Atmosphère %u.%u.%u-%s)\n", GetFatalConfig().GetFirmwareVersion().display_version, ATMOSPHERE_RELEASE_VERSION, ams::GetGitRevision());
fprintf(f_report, "Atmosphère Fatal Report (v1.1):\n");
fprintf(f_report, "Result: 0x%X (2%03d-%04d)\n\n", this->context->result.GetValue(), this->context->result.GetModule(), this->context->result.GetDescription());
fprintf(f_report, "Program ID: %016lx\n", static_cast<u64>(this->context->program_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, ATMOSPHERE_RELEASE_VERSION, ams::GetGitRevision());
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]);
if (this->context->cpu_ctx.architecture == CpuContext::Architecture_Aarch32) {
file.WriteFormat("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))) {
file.WriteFormat( " %3s: %08x\n", aarch32::CpuContext::RegisterNameStrings[i], this->context->cpu_ctx.aarch32_ctx.r[i]);
}
}
file.WriteFormat("Start Address: %08x\n", this->context->cpu_ctx.aarch32_ctx.base_address);
file.WriteFormat("Stack Trace:\n");
for (unsigned int i = 0; i < this->context->cpu_ctx.aarch32_ctx.stack_trace_size; i++) {
file.WriteFormat(" ReturnAddress[%02u]: %08x\n", i, this->context->cpu_ctx.aarch32_ctx.stack_trace[i]);
}
} else {
file.WriteFormat("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))) {
file.WriteFormat( " %3s: %016lx\n", aarch64::CpuContext::RegisterNameStrings[i], this->context->cpu_ctx.aarch64_ctx.x[i]);
}
}
file.WriteFormat("Start Address: %016lx\n", this->context->cpu_ctx.aarch64_ctx.base_address);
file.WriteFormat("Stack Trace:\n");
for (unsigned int i = 0; i < this->context->cpu_ctx.aarch64_ctx.stack_trace_size; i++) {
file.WriteFormat(" ReturnAddress[%02u]: %016lx\n", i, this->context->cpu_ctx.aarch64_ctx.stack_trace[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]);
if (this->context->stack_dump_size != 0) {
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",
this->context->stack_dump_base + ofs, this->context->stack_dump[ofs + 0], this->context->stack_dump[ofs + 1], this->context->stack_dump[ofs + 2], this->context->stack_dump[ofs + 3], this->context->stack_dump[ofs + 4], this->context->stack_dump[ofs + 5], this->context->stack_dump[ofs + 6], this->context->stack_dump[ofs + 7],
this->context->stack_dump[ofs + 8], this->context->stack_dump[ofs + 9], this->context->stack_dump[ofs + 10], this->context->stack_dump[ofs + 11], this->context->stack_dump[ofs + 12], this->context->stack_dump[ofs + 13], this->context->stack_dump[ofs + 14], this->context->stack_dump[ofs + 15]);
}
}
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]);
}
}
if (this->context->stack_dump_size != 0) {
fprintf(f_report, "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;
fprintf(f_report, " %012lx %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
this->context->stack_dump_base + ofs, this->context->stack_dump[ofs + 0], this->context->stack_dump[ofs + 1], this->context->stack_dump[ofs + 2], this->context->stack_dump[ofs + 3], this->context->stack_dump[ofs + 4], this->context->stack_dump[ofs + 5], this->context->stack_dump[ofs + 6], this->context->stack_dump[ofs + 7],
this->context->stack_dump[ofs + 8], this->context->stack_dump[ofs + 9], this->context->stack_dump[ofs + 10], this->context->stack_dump[ofs + 11], this->context->stack_dump[ofs + 12], this->context->stack_dump[ofs + 13], this->context->stack_dump[ofs + 14], this->context->stack_dump[ofs + 15]);
}
}
if (this->context->tls_address != 0) {
fprintf(f_report, "TLS Address: %016lx\n", this->context->tls_address);
fprintf(f_report, "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;
fprintf(f_report, " %012lx %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
this->context->tls_address + ofs, this->context->tls_dump[ofs + 0], this->context->tls_dump[ofs + 1], this->context->tls_dump[ofs + 2], this->context->tls_dump[ofs + 3], this->context->tls_dump[ofs + 4], this->context->tls_dump[ofs + 5], this->context->tls_dump[ofs + 6], this->context->tls_dump[ofs + 7],
this->context->tls_dump[ofs + 8], this->context->tls_dump[ofs + 9], this->context->tls_dump[ofs + 10], this->context->tls_dump[ofs + 11], this->context->tls_dump[ofs + 12], this->context->tls_dump[ofs + 13], this->context->tls_dump[ofs + 14], this->context->tls_dump[ofs + 15]);
if (this->context->tls_address != 0) {
file.WriteFormat("TLS Address: %016lx\n", this->context->tls_address);
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",
this->context->tls_address + ofs, this->context->tls_dump[ofs + 0], this->context->tls_dump[ofs + 1], this->context->tls_dump[ofs + 2], this->context->tls_dump[ofs + 3], this->context->tls_dump[ofs + 4], this->context->tls_dump[ofs + 5], this->context->tls_dump[ofs + 6], this->context->tls_dump[ofs + 7],
this->context->tls_dump[ofs + 8], this->context->tls_dump[ofs + 9], this->context->tls_dump[ofs + 10], this->context->tls_dump[ofs + 11], this->context->tls_dump[ofs + 12], this->context->tls_dump[ofs + 13], this->context->tls_dump[ofs + 14], this->context->tls_dump[ofs + 15]);
}
}
}
}
/* Dump data to file. */
{
snprintf(file_path, sizeof(file_path) - 1, "sdmc:/atmosphere/fatal_reports/dumps/%011lu_%016lx.bin", timestamp, static_cast<u64>(this->context->program_id));
FILE *f_dump = fopen(file_path, "wb");
if (f_dump == NULL) { return; }
ON_SCOPE_EXIT { fclose(f_dump); };
fwrite(this->context->tls_dump, sizeof(this->context->tls_dump), 1, f_dump);
if (this->context->stack_dump_size) {
fwrite(this->context->stack_dump, this->context->stack_dump_size, 1, f_dump);
std::snprintf(file_path, sizeof(file_path) - 1, "sdmc:/atmosphere/fatal_reports/dumps/%011lu_%016lx.bin", timestamp, static_cast<u64>(this->context->program_id));
ScopedFile file(file_path);
if (file.IsOpen()) {
file.Write(this->context->tls_dump, sizeof(this->context->tls_dump));
if (this->context->stack_dump_size) {
file.Write(this->context->stack_dump, this->context->stack_dump_size);
}
}
fflush(f_dump);
}
}