Atmosphere/stratosphere/creport/source/creport_crash_report.cpp

429 lines
19 KiB
C++
Raw Normal View History

/*
* Copyright (c) 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 <stratosphere.hpp>
#include "creport_crash_report.hpp"
#include "creport_utils.hpp"
namespace ams::creport {
namespace {
/* Convenience definitions. */
constexpr size_t DyingMessageAddressOffset = 0x1C0;
static_assert(DyingMessageAddressOffset == AMS_OFFSETOF(ams::svc::aarch64::ProcessLocalRegion, dying_message_region_address));
static_assert(DyingMessageAddressOffset == AMS_OFFSETOF(ams::svc::aarch32::ProcessLocalRegion, dying_message_region_address));
/* Helper functions. */
bool TryGetCurrentTimestamp(u64 *out) {
/* Clear output. */
*out = 0;
/* 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;
}
}
/* Try to get the current time. */
{
if (R_FAILED(::timeInitialize())) {
return false;
}
ON_SCOPE_EXIT { ::timeExit(); };
return R_SUCCEEDED(::timeGetCurrentTime(TimeType_LocalSystemClock, out));
}
}
2019-07-13 01:18:31 +00:00
void TryCreateReportDirectories() {
2020-03-08 09:45:12 +00:00
fs::EnsureDirectoryRecursively("sdmc:/atmosphere/crash_reports/dumps");
fs::EnsureDirectoryRecursively("sdmc:/atmosphere/fatal_reports/dumps");
}
2020-01-18 04:11:03 +00:00
constexpr const char *GetDebugExceptionString(const svc::DebugException type) {
switch (type) {
2020-01-18 04:11:03 +00:00
case svc::DebugException_UndefinedInstruction:
return "Undefined Instruction";
2020-01-18 04:11:03 +00:00
case svc::DebugException_InstructionAbort:
return "Instruction Abort";
2020-01-18 04:11:03 +00:00
case svc::DebugException_DataAbort:
return "Data Abort";
2020-01-18 04:11:03 +00:00
case svc::DebugException_AlignmentFault:
return "Alignment Fault";
2020-01-18 04:11:03 +00:00
case svc::DebugException_DebuggerAttached:
return "Debugger Attached";
2020-01-18 04:11:03 +00:00
case svc::DebugException_BreakPoint:
return "Break Point";
2020-01-18 04:11:03 +00:00
case svc::DebugException_UserBreak:
return "User Break";
2020-01-18 04:11:03 +00:00
case svc::DebugException_DebuggerBreak:
return "Debugger Break";
2020-01-18 04:11:03 +00:00
case svc::DebugException_UndefinedSystemCall:
return "Undefined System Call";
2020-01-18 04:11:03 +00:00
case svc::DebugException_MemorySystemError:
return "System Memory Error";
default:
return "Unknown";
}
}
}
2019-01-20 00:32:33 +00:00
void CrashReport::Initialize() {
/* Initialize the heap. */
2021-10-10 07:14:06 +00:00
m_heap_handle = lmem::CreateExpHeap(m_heap_storage, sizeof(m_heap_storage), lmem::CreateOption_None);
/* Allocate members. */
2021-10-10 07:14:06 +00:00
m_module_list = std::construct_at(static_cast<ModuleList *>(lmem::AllocateFromExpHeap(m_heap_handle, sizeof(ModuleList))));
m_thread_list = std::construct_at(static_cast<ThreadList *>(lmem::AllocateFromExpHeap(m_heap_handle, sizeof(ThreadList))));
m_dying_message = static_cast<u8 *>(lmem::AllocateFromExpHeap(m_heap_handle, DyingMessageSizeMax));
if (m_dying_message != nullptr) {
std::memset(m_dying_message, 0, DyingMessageSizeMax);
}
}
2019-10-18 02:48:28 +00:00
void CrashReport::BuildReport(os::ProcessId process_id, bool has_extra_info) {
2021-10-10 07:14:06 +00:00
m_has_extra_info = has_extra_info;
if (this->OpenProcess(process_id)) {
/* Parse info from the crashed process. */
this->ProcessExceptions();
m_module_list->FindModulesFromThreadInfo(m_debug_handle, m_crashed_thread, this->Is64Bit());
2021-10-10 07:14:06 +00:00
m_thread_list->ReadFromProcess(m_debug_handle, m_thread_tls_map, this->Is64Bit());
/* Associate module list to threads. */
2021-10-10 07:14:06 +00:00
m_crashed_thread.SetModuleList(m_module_list);
m_thread_list->SetModuleList(m_module_list);
/* Process dying message for applications. */
if (this->IsApplication()) {
this->ProcessDyingMessage();
}
/* Nintendo's creport finds extra modules by looking at all threads if application, */
/* but there's no reason for us not to always go looking. */
2021-10-10 07:14:06 +00:00
for (size_t i = 0; i < m_thread_list->GetThreadCount(); i++) {
m_module_list->FindModulesFromThreadInfo(m_debug_handle, m_thread_list->GetThreadInfo(i), this->Is64Bit());
}
/* Cache the module base address to send to fatal. */
2021-10-10 07:14:06 +00:00
if (m_module_list->GetModuleCount()) {
m_module_base_address = m_module_list->GetModuleStartAddress(0);
}
2019-07-13 01:18:31 +00:00
/* Nintendo's creport saves the report to erpt here, but we'll save to SD card later. */
}
2018-11-14 04:22:54 +00:00
}
void CrashReport::GetFatalContext(::FatalCpuContext *_out) const {
static_assert(sizeof(*_out) == sizeof(ams::fatal::CpuContext));
ams::fatal::CpuContext *out = reinterpret_cast<ams::fatal::CpuContext *>(_out);
std::memset(out, 0, sizeof(*out));
2019-07-13 01:18:31 +00:00
/* TODO: Support generating 32-bit fatal contexts? */
2019-07-19 02:09:35 +00:00
out->architecture = fatal::CpuContext::Architecture_Aarch64;
2021-10-10 07:14:06 +00:00
out->type = static_cast<u32>(m_exception_info.type);
2019-07-19 02:09:35 +00:00
for (size_t i = 0; i < fatal::aarch64::RegisterName_FP; i++) {
2021-10-10 07:14:06 +00:00
out->aarch64_ctx.SetRegisterValue(static_cast<fatal::aarch64::RegisterName>(i), m_crashed_thread.GetGeneralPurposeRegister(i));
}
2021-10-10 07:14:06 +00:00
out->aarch64_ctx.SetRegisterValue(fatal::aarch64::RegisterName_FP, m_crashed_thread.GetFP());
out->aarch64_ctx.SetRegisterValue(fatal::aarch64::RegisterName_LR, m_crashed_thread.GetLR());
out->aarch64_ctx.SetRegisterValue(fatal::aarch64::RegisterName_SP, m_crashed_thread.GetSP());
out->aarch64_ctx.SetRegisterValue(fatal::aarch64::RegisterName_PC, m_crashed_thread.GetPC());
2019-01-20 00:32:33 +00:00
2021-10-10 07:14:06 +00:00
out->aarch64_ctx.stack_trace_size = m_crashed_thread.GetStackTraceSize();
for (size_t i = 0; i < out->aarch64_ctx.stack_trace_size; i++) {
2021-10-10 07:14:06 +00:00
out->aarch64_ctx.stack_trace[i] = m_crashed_thread.GetStackTrace(i);
}
2021-10-10 07:14:06 +00:00
if (m_module_base_address != 0) {
out->aarch64_ctx.SetBaseAddress(m_module_base_address);
}
/* For ams fatal, which doesn't use afsr0, pass program_id instead. */
2021-10-10 07:14:06 +00:00
out->aarch64_ctx.SetProgramIdForAtmosphere(ncm::ProgramId{m_process_info.program_id});
}
void CrashReport::ProcessExceptions() {
/* Loop all debug events. */
svc::DebugEventInfo d;
2021-10-10 07:14:06 +00:00
while (R_SUCCEEDED(svc::GetDebugEvent(std::addressof(d), m_debug_handle))) {
switch (d.type) {
2020-07-31 08:27:09 +00:00
case svc::DebugEvent_CreateProcess:
this->HandleDebugEventInfoCreateProcess(d);
break;
2020-07-31 08:27:09 +00:00
case svc::DebugEvent_CreateThread:
this->HandleDebugEventInfoCreateThread(d);
break;
2020-01-18 04:11:03 +00:00
case svc::DebugEvent_Exception:
this->HandleDebugEventInfoException(d);
break;
2020-01-18 04:11:03 +00:00
case svc::DebugEvent_ExitProcess:
case svc::DebugEvent_ExitThread:
break;
}
}
/* Parse crashed thread info. */
2021-10-10 07:14:06 +00:00
m_crashed_thread.ReadFromProcess(m_debug_handle, m_thread_tls_map, m_crashed_thread_id, this->Is64Bit());
}
2020-07-31 08:27:09 +00:00
void CrashReport::HandleDebugEventInfoCreateProcess(const svc::DebugEventInfo &d) {
2021-10-10 07:14:06 +00:00
m_process_info = d.info.create_process;
/* On 5.0.0+, we want to parse out a dying message from application crashes. */
2020-04-14 05:19:44 +00:00
if (hos::GetVersion() < hos::Version_5_0_0 || !IsApplication()) {
return;
}
2019-01-20 00:32:33 +00:00
/* Parse out user data. */
2021-10-10 07:14:06 +00:00
const u64 address = m_process_info.user_exception_context_address + DyingMessageAddressOffset;
u64 userdata_address = 0;
u64 userdata_size = 0;
/* Read userdata address. */
2021-10-10 07:14:06 +00:00
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(userdata_address)), m_debug_handle, address, sizeof(userdata_address)))) {
return;
}
/* Validate userdata address. */
if (userdata_address == 0 || userdata_address & 0xFFF) {
return;
}
/* Read userdata size. */
2021-10-10 07:14:06 +00:00
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(userdata_size)), m_debug_handle, address + sizeof(userdata_address), sizeof(userdata_size)))) {
return;
}
/* Cap userdata size. */
userdata_size = std::min(size_t(userdata_size), DyingMessageSizeMax);
2021-10-10 07:14:06 +00:00
m_dying_message_address = userdata_address;
m_dying_message_size = userdata_size;
}
2020-07-31 08:27:09 +00:00
void CrashReport::HandleDebugEventInfoCreateThread(const svc::DebugEventInfo &d) {
/* Save info on the thread's TLS address for later. */
2021-10-10 07:14:06 +00:00
m_thread_tls_map.SetThreadTls(d.info.create_thread.thread_id, d.info.create_thread.tls_address);
}
void CrashReport::HandleDebugEventInfoException(const svc::DebugEventInfo &d) {
switch (d.info.exception.type) {
2020-01-18 04:11:03 +00:00
case svc::DebugException_UndefinedInstruction:
2021-10-10 07:14:06 +00:00
m_result = creport::ResultUndefinedInstruction();
break;
2020-01-18 04:11:03 +00:00
case svc::DebugException_InstructionAbort:
2021-10-10 07:14:06 +00:00
m_result = creport::ResultInstructionAbort();
break;
2020-01-18 04:11:03 +00:00
case svc::DebugException_DataAbort:
2021-10-10 07:14:06 +00:00
m_result = creport::ResultDataAbort();
break;
2020-01-18 04:11:03 +00:00
case svc::DebugException_AlignmentFault:
2021-10-10 07:14:06 +00:00
m_result = creport::ResultAlignmentFault();
break;
2020-01-18 04:11:03 +00:00
case svc::DebugException_UserBreak:
2021-10-10 07:14:06 +00:00
m_result = creport::ResultUserBreak();
/* Try to parse out the user break result. */
2020-04-14 05:19:44 +00:00
if (hos::GetVersion() >= hos::Version_5_0_0) {
2021-10-10 07:14:06 +00:00
svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(m_result)), m_debug_handle, d.info.exception.specific.user_break.address, sizeof(m_result));
}
break;
2020-01-18 04:11:03 +00:00
case svc::DebugException_UndefinedSystemCall:
2021-10-10 07:14:06 +00:00
m_result = creport::ResultUndefinedSystemCall();
break;
2020-01-18 04:11:03 +00:00
case svc::DebugException_MemorySystemError:
2021-10-10 07:14:06 +00:00
m_result = creport::ResultMemorySystemError();
break;
2020-01-18 04:11:03 +00:00
case svc::DebugException_DebuggerAttached:
case svc::DebugException_BreakPoint:
case svc::DebugException_DebuggerBreak:
return;
}
/* Save exception info. */
2021-10-10 07:14:06 +00:00
m_exception_info = d.info.exception;
m_crashed_thread_id = d.thread_id;
}
void CrashReport::ProcessDyingMessage() {
/* Dying message is only stored starting in 5.0.0. */
2020-04-14 05:19:44 +00:00
if (hos::GetVersion() < hos::Version_5_0_0) {
return;
}
/* Validate address/size. */
2021-10-10 07:14:06 +00:00
if (m_dying_message_address == 0 || m_dying_message_address & 0xFFF) {
return;
}
2021-10-10 07:14:06 +00:00
if (m_dying_message_size > DyingMessageSizeMax) {
return;
}
2019-01-20 00:32:33 +00:00
/* Validate that the current report isn't garbage. */
if (!IsOpen() || !IsComplete()) {
return;
}
/* Verify that we have a dying message buffer. */
2021-10-10 07:14:06 +00:00
if (m_dying_message == nullptr) {
return;
}
/* Read the dying message. */
2021-10-10 07:14:06 +00:00
svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(m_dying_message), m_debug_handle, m_dying_message_address, m_dying_message_size);
2018-06-26 06:44:58 +00:00
}
2020-12-15 03:15:49 +00:00
void CrashReport::SaveReport(bool enable_screenshot) {
2019-07-13 01:18:31 +00:00
/* Try to ensure path exists. */
TryCreateReportDirectories();
2019-01-20 00:32:33 +00:00
/* Get a timestamp. */
u64 timestamp;
if (!TryGetCurrentTimestamp(&timestamp)) {
2021-10-04 21:54:13 +00:00
timestamp = os::GetSystemTick().GetInt64Value();
}
/* Save files. */
{
2020-03-08 09:45:12 +00:00
char file_path[fs::EntryNameLengthMax + 1];
/* Save crash report. */
2021-10-10 07:14:06 +00:00
util::SNPrintf(file_path, sizeof(file_path), "sdmc:/atmosphere/crash_reports/%011lu_%016lx.log", timestamp, m_process_info.program_id);
2020-03-08 09:45:12 +00:00
{
ScopedFile file(file_path);
if (file.IsOpen()) {
this->SaveToFile(file);
}
2018-06-26 06:44:58 +00:00
}
/* Dump threads. */
2021-10-10 07:14:06 +00:00
util::SNPrintf(file_path, sizeof(file_path), "sdmc:/atmosphere/crash_reports/dumps/%011lu_%016lx_thread_info.bin", timestamp, m_process_info.program_id);
2020-03-08 09:45:12 +00:00
{
ScopedFile file(file_path);
if (file.IsOpen()) {
2021-10-10 07:14:06 +00:00
m_thread_list->DumpBinary(file, m_crashed_thread.GetThreadId());
}
}
/* If we're open, we need to close here. */
if (this->IsOpen()) {
this->Close();
}
/* Finalize our heap. */
2021-10-10 07:14:06 +00:00
std::destroy_at(m_module_list);
std::destroy_at(m_thread_list);
lmem::FreeToExpHeap(m_heap_handle, m_module_list);
lmem::FreeToExpHeap(m_heap_handle, m_thread_list);
if (m_dying_message != nullptr) {
lmem::FreeToExpHeap(m_heap_handle, m_dying_message);
}
2021-10-10 07:14:06 +00:00
m_module_list = nullptr;
m_thread_list = nullptr;
m_dying_message = nullptr;
/* Try to take a screenshot. */
2020-12-15 03:15:49 +00:00
/* NOTE: Nintendo validates that enable_screenshot is true here, and validates that the application id is not in a blacklist. */
/* Since we save reports only locally and do not send them via telemetry, we will skip this. */
AMS_UNUSED(enable_screenshot);
if (hos::GetVersion() >= hos::Version_9_0_0 && this->IsApplication()) {
if (R_SUCCEEDED(capsrv::InitializeScreenShotControl())) {
ON_SCOPE_EXIT { capsrv::FinalizeScreenShotControl(); };
u64 jpeg_size;
2021-10-10 07:14:06 +00:00
if (R_SUCCEEDED(capsrv::CaptureJpegScreenshot(std::addressof(jpeg_size), m_heap_storage, sizeof(m_heap_storage), vi::LayerStack_ApplicationForDebug, TimeSpan::FromSeconds(10)))) {
util::SNPrintf(file_path, sizeof(file_path), "sdmc:/atmosphere/crash_reports/%011lu_%016lx.jpg", timestamp, m_process_info.program_id);
ScopedFile file(file_path);
if (file.IsOpen()) {
2021-10-10 07:14:06 +00:00
file.Write(m_heap_storage, jpeg_size);
}
}
2020-03-08 09:45:12 +00:00
}
}
}
2018-06-26 06:44:58 +00:00
}
2020-03-08 09:45:12 +00:00
void CrashReport::SaveToFile(ScopedFile &file) {
file.WriteFormat("Atmosphère Crash Report (v1.7):\n");
2021-10-10 07:14:06 +00:00
file.WriteFormat("Result: 0x%X (2%03d-%04d)\n\n", m_result.GetValue(), m_result.GetModule(), m_result.GetDescription());
/* Process Info. */
char name_buf[0x10] = {};
2021-10-10 07:14:06 +00:00
static_assert(sizeof(name_buf) >= sizeof(m_process_info.name), "buffer overflow!");
std::memcpy(name_buf, m_process_info.name, sizeof(m_process_info.name));
2020-03-08 09:45:12 +00:00
file.WriteFormat("Process Info:\n");
file.WriteFormat(" Process Name: %s\n", name_buf);
2021-10-10 07:14:06 +00:00
file.WriteFormat(" Program ID: %016lx\n", m_process_info.program_id);
file.WriteFormat(" Process ID: %016lx\n", m_process_info.process_id);
file.WriteFormat(" Process Flags: %08x\n", m_process_info.flags);
2020-04-14 05:19:44 +00:00
if (hos::GetVersion() >= hos::Version_5_0_0) {
2021-10-10 07:14:06 +00:00
file.WriteFormat(" User Exception Address: %s\n", m_module_list->GetFormattedAddressString(m_process_info.user_exception_context_address));
2018-06-26 06:44:58 +00:00
}
/* Exception Info. */
2020-03-08 09:45:12 +00:00
file.WriteFormat("Exception Info:\n");
2021-10-10 07:14:06 +00:00
file.WriteFormat(" Type: %s\n", GetDebugExceptionString(m_exception_info.type));
file.WriteFormat(" Address: %s\n", m_module_list->GetFormattedAddressString(m_exception_info.address));
switch (m_exception_info.type) {
2020-01-18 04:11:03 +00:00
case svc::DebugException_UndefinedInstruction:
2021-10-10 07:14:06 +00:00
file.WriteFormat(" Opcode: %08x\n", m_exception_info.specific.undefined_instruction.insn);
break;
2020-01-18 04:11:03 +00:00
case svc::DebugException_DataAbort:
case svc::DebugException_AlignmentFault:
2021-10-10 07:14:06 +00:00
if (m_exception_info.specific.raw != m_exception_info.address) {
file.WriteFormat(" Fault Address: %s\n", m_module_list->GetFormattedAddressString(m_exception_info.specific.raw));
}
break;
2020-01-18 04:11:03 +00:00
case svc::DebugException_UndefinedSystemCall:
2021-10-10 07:14:06 +00:00
file.WriteFormat(" Svc Id: 0x%02x\n", m_exception_info.specific.undefined_system_call.id);
break;
2020-01-18 04:11:03 +00:00
case svc::DebugException_UserBreak:
2021-10-10 07:14:06 +00:00
file.WriteFormat(" Break Reason: 0x%x\n", m_exception_info.specific.user_break.break_reason);
file.WriteFormat(" Break Address: %s\n", m_module_list->GetFormattedAddressString(m_exception_info.specific.user_break.address));
file.WriteFormat(" Break Size: 0x%lx\n", m_exception_info.specific.user_break.size);
break;
default:
break;
2018-06-26 06:44:58 +00:00
}
/* Crashed Thread Info. */
2020-03-08 09:45:12 +00:00
file.WriteFormat("Crashed Thread Info:\n");
2021-10-10 07:14:06 +00:00
m_crashed_thread.SaveToFile(file);
/* Dying Message. */
2021-10-10 07:14:06 +00:00
if (hos::GetVersion() >= hos::Version_5_0_0 && m_dying_message_size != 0) {
2020-03-08 09:45:12 +00:00
file.WriteFormat("Dying Message Info:\n");
2021-10-10 07:14:06 +00:00
file.WriteFormat(" Address: 0x%s\n", m_module_list->GetFormattedAddressString(m_dying_message_address));
file.WriteFormat(" Size: 0x%016lx\n", m_dying_message_size);
file.DumpMemory( " Dying Message: ", m_dying_message, m_dying_message_size);
2018-06-26 06:44:58 +00:00
}
/* Module Info. */
2020-03-08 09:45:12 +00:00
file.WriteFormat("Module Info:\n");
2021-10-10 07:14:06 +00:00
m_module_list->SaveToFile(file);
/* Thread Info. */
2020-03-08 10:08:39 +00:00
file.WriteFormat("Thread Report:\n");
2021-10-10 07:14:06 +00:00
m_thread_list->SaveToFile(file);
2018-06-26 06:44:58 +00:00
}
2018-06-26 06:44:58 +00:00
}