Fatal: save auto-debug info to SD card.

This commit is contained in:
Michael Scire 2018-11-14 14:13:31 -08:00
parent 962fa0a690
commit 8054b2d219
8 changed files with 151 additions and 6 deletions

View file

@ -257,7 +257,7 @@ void CrashReport::EnsureReportDirectories() {
}
void CrashReport::SaveReport() {
/* TODO: Save the report to the SD card. */
/* Save the report to the SD card. */
char report_path[FS_MAX_PATH];
/* Ensure path exists. */

View file

@ -15,7 +15,7 @@
"permissions": "0xFFFFFFFFFFFFFFFF"
},
"service_access": ["bpc", "bpc:c", "erpt:c", "fsp-srv", "gpio", "i2c", "lbl", "lm", "nvdrv:s", "pcv", "pl:u", "pm:info", "psm", "set", "set:sys", "spsm", "vi:m", "vi:s"],
"service_host": ["fatal:p", "fatal:u"],
"service_host": ["fatal:p", "fatal:u", "time:s"],
"kernel_capabilities": [{
"type": "kernel_flags",
"value": {

View file

@ -146,6 +146,7 @@ void TryCollectDebugInformation(FatalThrowContext *ctx, u64 pid) {
while (R_SUCCEEDED(svcGetDebugEvent((u8 *)&d, debug_handle))) {
if (d.type == DebugEventType::AttachProcess) {
ctx->cpu_ctx.is_aarch32 = (d.info.attach_process.flags & 1) == 0;
memcpy(ctx->proc_name, d.info.attach_process.name, sizeof(d.info.attach_process.name));
got_attach_process = true;
} else if (d.type == DebugEventType::AttachThread) {
thread_id_to_tls[d.info.attach_thread.thread_id] = d.info.attach_thread.tls_address;
@ -228,6 +229,16 @@ void TryCollectDebugInformation(FatalThrowContext *ctx, u64 pid) {
cur_fp = cur_frame.fp;
}
/* Try to read up to 0x100 of stack. */
for (size_t sz = 0x100; sz > 0; sz -= 0x10) {
if (IsAddressReadable(debug_handle, ctx->cpu_ctx.aarch64_ctx.sp, sz, nullptr)) {
if (R_SUCCEEDED(svcReadDebugProcessMemory(ctx->stack_dump, debug_handle, ctx->cpu_ctx.aarch64_ctx.sp, sz))) {
ctx->stack_dump_size = sz;
}
break;
}
}
/* Parse the starting address. */
{
u64 guess = thread_ctx.pc.x;

View file

@ -35,7 +35,7 @@ extern "C" {
u32 __nx_applet_type = AppletType_None;
#define INNER_HEAP_SIZE 0x280000
#define INNER_HEAP_SIZE 0x2A0000
size_t nx_inner_heap_size = INNER_HEAP_SIZE;
char nx_inner_heap[INNER_HEAP_SIZE];
@ -118,11 +118,28 @@ void __appInit(void) {
std::abort();
}
rc = fsInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = fsInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = fsdevMountSdmc();
if (R_FAILED(rc)) {
std::abort();
}
/* fatal cannot throw fatal, so don't do: CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION); */
}
void __appExit(void) {
/* Cleanup services. */
fsdevUnmountAll();
fsExit();
plExit();
spsmExit();
psmExit();

View file

@ -14,17 +14,127 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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"
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);
}
bool ErrorReportTask::GetCurrentTime(u64 *out) {
*out = 0;
/* Verify that pcv isn't dead. */
{
Handle dummy;
if (R_SUCCEEDED(smRegisterService(&dummy, "time:s", false, 0x20))) {
svcCloseHandle(dummy);
return false;
}
}
/* Try to get the current time. */
bool success = false;
if (R_SUCCEEDED(timeInitialize())) {
if (R_SUCCEEDED(timeGetCurrentTime(TimeType_LocalSystemClock, out))) {
success = true;
}
timeExit();
}
return success;
}
void ErrorReportTask::SaveReportToSdCard() {
char file_path[FS_MAX_PATH];
/* Ensure path exists. */
EnsureReportDirectories();
/* Get a timestamp. */
u64 timestamp;
if (!GetCurrentTime(&timestamp)) {
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]);
}
}
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]);
}
} 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]);
}
}
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->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);
}
}
Result ErrorReportTask::Run() {
if (this->create_report) {
/* Here, Nintendo creates an error report with erpt. AMS will not do that. */
/* TODO: Should atmosphere log reports to to the SD card? */
}
/* Save report to SD card. */
if (!this->ctx->is_creport) {
SaveReportToSdCard();
}
/* Signal we're done with our job. */
eventFire(this->erpt_event);
return 0;
}

View file

@ -23,6 +23,10 @@ class ErrorReportTask : public IFatalTask {
private:
bool create_report;
Event *erpt_event;
private:
void EnsureReportDirectories();
bool GetCurrentTime(u64 *out);
void SaveReportToSdCard();
public:
ErrorReportTask(FatalThrowContext *ctx, u64 title_id, bool error_report, Event *evt) : IFatalTask(ctx, title_id), create_report(error_report), erpt_event(evt) { }
virtual Result Run() override;

View file

@ -42,7 +42,7 @@ Result ThrowFatalForSelf(u32 error) {
Result ThrowFatalImpl(u32 error, u64 pid, FatalType policy, FatalCpuContext *cpu_ctx) {
Result rc = 0;
FatalThrowContext ctx;
FatalThrowContext ctx = {0};
ctx.error_code = error;
if (cpu_ctx != nullptr) {
ctx.cpu_ctx = *cpu_ctx;

View file

@ -100,6 +100,9 @@ struct FatalThrowContext {
u32 error_code;
bool is_creport;
bool has_gprs[NumAarch64Gprs];
size_t stack_dump_size;
u8 stack_dump[0x100];
char proc_name[0xD];
FatalCpuContext cpu_ctx;
};