2018-09-07 15:00:13 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 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/>.
|
|
|
|
*/
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2018-06-25 04:46:20 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
#include <switch.h>
|
2019-02-20 22:23:38 +00:00
|
|
|
#include <stratosphere/firmware_version.hpp>
|
2018-06-25 04:46:20 +00:00
|
|
|
|
2018-06-25 06:42:26 +00:00
|
|
|
#include "creport_crash_report.hpp"
|
|
|
|
|
2018-06-25 04:46:20 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
extern u32 __start__;
|
|
|
|
|
|
|
|
u32 __nx_applet_type = AppletType_None;
|
|
|
|
|
|
|
|
#define INNER_HEAP_SIZE 0x100000
|
|
|
|
size_t nx_inner_heap_size = INNER_HEAP_SIZE;
|
|
|
|
char nx_inner_heap[INNER_HEAP_SIZE];
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2018-06-25 04:46:20 +00:00
|
|
|
void __libnx_initheap(void);
|
|
|
|
void __appInit(void);
|
|
|
|
void __appExit(void);
|
2019-04-12 22:28:46 +00:00
|
|
|
|
|
|
|
/* Exception handling. */
|
|
|
|
alignas(16) u8 __nx_exception_stack[0x1000];
|
|
|
|
u64 __nx_exception_stack_size = sizeof(__nx_exception_stack);
|
|
|
|
void __libnx_exception_handler(ThreadExceptionDump *ctx);
|
|
|
|
void __libstratosphere_exception_handler(AtmosphereFatalErrorContext *ctx);
|
|
|
|
}
|
|
|
|
|
2019-07-03 03:54:16 +00:00
|
|
|
sts::ncm::TitleId __stratosphere_title_id = sts::ncm::TitleId::Creport;
|
|
|
|
|
2019-04-12 22:28:46 +00:00
|
|
|
void __libnx_exception_handler(ThreadExceptionDump *ctx) {
|
|
|
|
StratosphereCrashHandler(ctx);
|
2018-06-25 04:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void __libnx_initheap(void) {
|
|
|
|
void* addr = nx_inner_heap;
|
|
|
|
size_t size = nx_inner_heap_size;
|
|
|
|
|
|
|
|
/* Newlib */
|
|
|
|
extern char* fake_heap_start;
|
|
|
|
extern char* fake_heap_end;
|
|
|
|
|
|
|
|
fake_heap_start = (char*)addr;
|
|
|
|
fake_heap_end = (char*)addr + size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __appInit(void) {
|
2019-02-20 22:17:51 +00:00
|
|
|
SetFirmwareVersionForLibnx();
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2019-04-22 19:40:53 +00:00
|
|
|
DoWithSmSession([&]() {
|
2019-06-20 09:00:59 +00:00
|
|
|
R_ASSERT(fsInitialize());
|
2019-04-22 19:40:53 +00:00
|
|
|
});
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2019-06-20 09:00:59 +00:00
|
|
|
R_ASSERT(fsdevMountSdmc());
|
2018-06-25 04:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void __appExit(void) {
|
|
|
|
/* Cleanup services. */
|
|
|
|
fsdevUnmountAll();
|
|
|
|
fsExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 creport_parse_u64(char *s) {
|
|
|
|
/* Official creport uses this custom parsing logic... */
|
|
|
|
u64 out_val = 0;
|
|
|
|
for (unsigned int i = 0; i < 20 && s[i]; i++) {
|
|
|
|
if ('0' <= s[i] && s[i] <= '9') {
|
|
|
|
out_val *= 10;
|
|
|
|
out_val += (s[i] - '0');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out_val;
|
|
|
|
}
|
|
|
|
|
2018-06-25 06:42:26 +00:00
|
|
|
static CrashReport g_Creport;
|
|
|
|
|
2018-06-25 04:46:20 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
/* Validate arguments. */
|
|
|
|
if (argc < 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
if (argv[i] == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2018-06-25 06:42:26 +00:00
|
|
|
/* Parse crashed PID. */
|
2018-06-25 04:46:20 +00:00
|
|
|
u64 crashed_pid = creport_parse_u64(argv[0]);
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2018-06-25 06:42:26 +00:00
|
|
|
/* Try to debug the crashed process. */
|
|
|
|
g_Creport.BuildReport(crashed_pid, argv[1][0] == '1');
|
2018-06-25 07:58:44 +00:00
|
|
|
if (g_Creport.WasSuccessful()) {
|
2018-06-25 16:22:37 +00:00
|
|
|
g_Creport.SaveReport();
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2019-04-22 19:40:53 +00:00
|
|
|
DoWithSmSession([&]() {
|
|
|
|
if (R_SUCCEEDED(nsdevInitialize())) {
|
|
|
|
nsdevTerminateProcess(crashed_pid);
|
|
|
|
nsdevExit();
|
|
|
|
}
|
|
|
|
});
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2018-06-25 07:58:44 +00:00
|
|
|
/* Don't fatal if we have extra info. */
|
2019-05-10 10:25:07 +00:00
|
|
|
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_500)) {
|
2018-06-25 07:58:44 +00:00
|
|
|
if (g_Creport.IsApplication()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (argv[1][0] == '1') {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2018-06-25 07:58:44 +00:00
|
|
|
/* Also don't fatal if we're a user break. */
|
|
|
|
if (g_Creport.IsUserBreak()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2018-11-14 04:22:54 +00:00
|
|
|
FatalContext *ctx = g_Creport.GetFatalContext();
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2018-11-14 04:22:54 +00:00
|
|
|
fatalWithContext(g_Creport.GetResult(), FatalType_ErrorScreen, ctx);
|
2018-06-25 04:46:20 +00:00
|
|
|
}
|
2019-05-25 20:32:34 +00:00
|
|
|
|
2018-06-25 04:46:20 +00:00
|
|
|
}
|