2018-09-07 15:00:13 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2018-09-07 15:00:13 +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/>.
|
|
|
|
*/
|
2020-05-11 22:02:10 +00:00
|
|
|
#include <stratosphere.hpp>
|
2018-06-25 06:42:26 +00:00
|
|
|
#include "creport_crash_report.hpp"
|
2019-07-12 12:31:00 +00:00
|
|
|
#include "creport_utils.hpp"
|
2018-06-25 06:42:26 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
namespace ams {
|
2018-06-25 04:46:20 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
namespace creport {
|
2018-06-25 04:46:20 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
namespace {
|
2018-06-25 04:46:20 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
constinit u8 g_fs_heap_memory[4_KB];
|
|
|
|
lmem::HeapHandle g_fs_heap_handle;
|
2021-01-19 01:50:40 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
void *AllocateForFs(size_t size) {
|
|
|
|
return lmem::AllocateFromExpHeap(g_fs_heap_handle, size);
|
|
|
|
}
|
2021-01-19 01:50:40 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
void DeallocateForFs(void *p, size_t size) {
|
|
|
|
AMS_UNUSED(size);
|
|
|
|
return lmem::FreeToExpHeap(g_fs_heap_handle, p);
|
|
|
|
}
|
2021-01-19 01:50:40 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
void InitializeFsHeap() {
|
|
|
|
g_fs_heap_handle = lmem::CreateExpHeap(g_fs_heap_memory, sizeof(g_fs_heap_memory), lmem::CreateOption_None);
|
|
|
|
}
|
2021-01-19 01:50:40 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
}
|
2021-01-19 01:50:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
namespace init {
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
void InitializeSystemModule() {
|
|
|
|
/* Initialize heap. */
|
|
|
|
creport::InitializeFsHeap();
|
2021-01-19 01:50:40 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* Initialize our connection to sm. */
|
|
|
|
R_ABORT_UNLESS(sm::Initialize());
|
2021-04-14 06:58:10 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* Initialize fs. */
|
|
|
|
fs::InitializeForSystem();
|
|
|
|
fs::SetAllocator(creport::AllocateForFs, creport::DeallocateForFs);
|
|
|
|
fs::SetEnabledAutoAbort(false);
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* Mount the SD card. */
|
|
|
|
R_ABORT_UNLESS(fs::MountSdCard("sdmc"));
|
|
|
|
}
|
2018-06-25 04:46:20 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
void FinalizeSystemModule() { /* ... */ }
|
2018-06-25 04:46:20 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
void Startup() { /* ... */ }
|
2021-01-19 01:50:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
namespace {
|
2021-01-19 01:50:40 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
constinit creport::CrashReport g_crash_report;
|
2021-01-19 01:50:40 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
}
|
2021-01-19 01:50:40 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
void Main() {
|
|
|
|
/* Set thread name. */
|
|
|
|
os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(creport, Main));
|
|
|
|
AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(creport, Main));
|
2018-06-25 06:42:26 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* Get arguments. */
|
|
|
|
const int num_args = os::GetHostArgc();
|
|
|
|
char ** const args = os::GetHostArgv();
|
2020-04-17 08:06:07 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* Validate arguments. */
|
|
|
|
if (num_args < 2) {
|
|
|
|
return;
|
2018-06-25 04:46:20 +00:00
|
|
|
}
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
for (auto i = 0; i < num_args; ++i) {
|
|
|
|
if (args[i] == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* Parse arguments. */
|
|
|
|
const os::ProcessId crashed_pid = creport::ParseProcessIdArgument(args[0]);
|
|
|
|
const bool has_extra_info = args[1][0] == '1';
|
|
|
|
const bool enable_screenshot = num_args >= 3 && args[2][0] == '1';
|
|
|
|
const bool enable_jit_debug = num_args >= 4 && args[3][0] == '1';
|
2020-04-22 21:50:16 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* Initialize the crash report. */
|
|
|
|
g_crash_report.Initialize();
|
2019-07-12 12:31:00 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* Try to debug the crashed process. */
|
|
|
|
g_crash_report.BuildReport(crashed_pid, has_extra_info);
|
|
|
|
if (!g_crash_report.IsComplete()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-14 06:58:10 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* Save report to file. */
|
|
|
|
g_crash_report.SaveReport(enable_screenshot);
|
|
|
|
|
|
|
|
/* Try to terminate the process, if we should. */
|
|
|
|
const auto fw_ver = hos::GetVersion();
|
|
|
|
if (fw_ver < hos::Version_11_0_0 || !enable_jit_debug) {
|
|
|
|
if (fw_ver >= hos::Version_10_0_0) {
|
|
|
|
/* Use pgl to terminate. */
|
|
|
|
if (R_SUCCEEDED(pgl::Initialize())) {
|
|
|
|
ON_SCOPE_EXIT { pgl::Finalize(); };
|
|
|
|
|
|
|
|
pgl::TerminateProcess(crashed_pid);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Use ns to terminate. */
|
|
|
|
if (R_SUCCEEDED(::nsdevInitialize())) {
|
|
|
|
ON_SCOPE_EXIT { ::nsdevExit(); };
|
|
|
|
|
|
|
|
nsdevTerminateProcess(crashed_pid.value);
|
|
|
|
}
|
2020-12-15 03:15:49 +00:00
|
|
|
}
|
2021-10-08 00:44:54 +00:00
|
|
|
}
|
2021-04-14 06:58:10 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* If we're on 5.0.0+ and an application crashed, or if we have extra info, we don't need to fatal. */
|
|
|
|
if (fw_ver >= hos::Version_5_0_0) {
|
|
|
|
if (g_crash_report.IsApplication()) {
|
|
|
|
return;
|
2020-12-15 03:15:49 +00:00
|
|
|
}
|
2021-10-08 00:44:54 +00:00
|
|
|
} else if (has_extra_info) {
|
|
|
|
return;
|
2018-06-25 07:58:44 +00:00
|
|
|
}
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* We also don't need to fatal on user break. */
|
|
|
|
if (g_crash_report.IsUserBreak()) {
|
|
|
|
return;
|
2019-07-12 12:31:00 +00:00
|
|
|
}
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2021-10-08 00:44:54 +00:00
|
|
|
/* Throw fatal error. */
|
|
|
|
{
|
|
|
|
::FatalCpuContext ctx;
|
|
|
|
g_crash_report.GetFatalContext(std::addressof(ctx));
|
|
|
|
fatalThrowWithContext(g_crash_report.GetResult().GetValue(), FatalPolicy_ErrorScreen, std::addressof(ctx));
|
|
|
|
}
|
2018-06-25 04:46:20 +00:00
|
|
|
}
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2019-07-12 12:31:00 +00:00
|
|
|
}
|