abort/error: print backtrace, abuse templates, overhaul result/diag (macos not done yet)

This commit is contained in:
Michael Scire 2022-03-10 01:15:45 -08:00 committed by SciresM
parent 18168d54c3
commit 646f84bad1
118 changed files with 2843 additions and 369 deletions

View file

@ -20,6 +20,10 @@
#include <stratosphere/diag/impl/diag_impl_log.hpp>
#include <stratosphere/diag/diag_log.hpp>
#include <stratosphere/diag/diag_sdk_log.hpp>
#include <stratosphere/diag/diag_abort_observer.hpp>
#include <stratosphere/diag/diag_assertion_failure_handler.hpp>
#include <stratosphere/diag/impl/diag_utf8_util.hpp>
#include <stratosphere/diag/diag_backtrace.hpp>
#include <stratosphere/diag/diag_symbol.hpp>

View file

@ -0,0 +1,55 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/diag/diag_log_types.hpp>
namespace ams::diag {
using AbortObserver = void (*)(const AbortInfo &);
struct AbortObserverHolder {
AbortObserver observer;
AbortObserverHolder *next;
bool is_registered;
};
void InitializeAbortObserverHolder(AbortObserverHolder *holder, AbortObserver observer);
void RegisterAbortObserver(AbortObserverHolder *holder);
void UnregisterAbortObserver(AbortObserverHolder *holder);
void EnableDefaultAbortObserver(bool en);
struct SdkAbortInfo {
AbortInfo abort_info;
Result result;
const ::ams::os::UserExceptionInfo *exc_info;
};
using SdkAbortObserver = void (*)(const SdkAbortInfo &);
struct SdkAbortObserverHolder {
SdkAbortObserver observer;
SdkAbortObserverHolder *next;
bool is_registered;
};
void InitializeSdkAbortObserverHolder(SdkAbortObserverHolder *holder, SdkAbortObserver observer);
void RegisterSdkAbortObserver(SdkAbortObserverHolder *holder);
void UnregisterSdkAbortObserver(SdkAbortObserverHolder *holder);
}

View file

@ -0,0 +1,31 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/diag/diag_log_types.hpp>
namespace ams::diag {
enum AssertionFailureOperation {
AssertionFailureOperation_Abort,
AssertionFailureOperation_Continue,
};
using AssertionFailureHandler = AssertionFailureOperation (*)(const AssertionInfo &info);
void SetAssertionFailureHandler(AssertionFailureHandler handler);
}

View file

@ -0,0 +1,60 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#if defined(ATMOSPHERE_OS_HORIZON)
#include <stratosphere/diag/impl/diag_backtrace_impl.os.horizon.hpp>
#elif defined(ATMOSPHERE_OS_WINDOWS)
#include <stratosphere/diag/impl/diag_backtrace_impl.os.windows.hpp>
#elif defined(ATMOSPHERE_OS_LINUX)
#include <stratosphere/diag/impl/diag_backtrace_impl.os.linux.hpp>
#elif defined(ATMOSPHERE_OS_MACOS)
#include <stratosphere/diag/impl/diag_backtrace_impl.os.macos.hpp>
#else
#error "Unknown OS for diag::Backtrace"
#endif
namespace ams::diag {
size_t GetBacktrace(uintptr_t *out, size_t out_size);
#if defined(ATMOSPHERE_OS_HORIZON)
size_t GetBacktace(uintptr_t *out, size_t out_size, uintptr_t fp, uintptr_t sp, uintptr_t pc);
#endif
class Backtrace {
private:
impl::Backtrace m_impl;
public:
NOINLINE Backtrace() {
m_impl.Initialize();
m_impl.Step();
}
#if defined(ATMOSPHERE_OS_HORIZON)
Backtrace(uintptr_t fp, uintptr_t sp, uintptr_t pc) {
m_impl.Initialize(fp, sp, pc);
}
#endif
bool Step() { return m_impl.Step(); }
uintptr_t GetStackPointer() const { return m_impl.GetStackPointer(); }
uintptr_t GetReturnAddress() const { return m_impl.GetReturnAddress(); }
};
}

View file

@ -0,0 +1,23 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::diag {
uintptr_t GetSymbolName(char *dst, size_t dst_size, uintptr_t address);
size_t GetSymbolSize(uintptr_t address);
}

View file

@ -0,0 +1,50 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::diag::impl {
class Backtrace {
private:
static constexpr size_t MemoryInfoBufferSize = 5;
public:
struct StackInfo {
uintptr_t stack_top;
uintptr_t stack_bottom;
};
private:
s64 m_memory_info_buffer[MemoryInfoBufferSize]{};
StackInfo *m_current_stack_info = nullptr;
StackInfo m_exception_stack_info{};
StackInfo m_normal_stack_info{};
uintptr_t m_fp = 0;
uintptr_t m_prev_fp = 0;
uintptr_t m_lr = 0;
public:
Backtrace() = default;
void Initialize();
void Initialize(uintptr_t fp, uintptr_t sp, uintptr_t pc);
bool Step();
uintptr_t GetStackPointer() const;
uintptr_t GetReturnAddress() const;
private:
void SetStackInfo(uintptr_t fp, uintptr_t sp);
};
}

View file

@ -0,0 +1,38 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::diag::impl {
class Backtrace {
private:
static constexpr size_t BacktraceEntryCountMax = 0x80;
private:
void *m_backtrace_addresses[BacktraceEntryCountMax];
size_t m_index = 0;
size_t m_size = 0;
public:
Backtrace() = default;
void Initialize();
bool Step();
uintptr_t GetStackPointer() const;
uintptr_t GetReturnAddress() const;
};
}

View file

@ -0,0 +1,38 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::diag::impl {
class Backtrace {
private:
static constexpr size_t BacktraceEntryCountMax = 0x80;
private:
void *m_backtrace_addresses[BacktraceEntryCountMax];
size_t m_index = 0;
size_t m_size = 0;
public:
Backtrace() = default;
void Initialize();
bool Step();
uintptr_t GetStackPointer() const;
uintptr_t GetReturnAddress() const;
};
}

View file

@ -0,0 +1,38 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::diag::impl {
class Backtrace {
private:
static constexpr size_t BacktraceEntryCountMax = 0x80;
private:
void *m_backtrace_addresses[BacktraceEntryCountMax];
size_t m_index = 0;
size_t m_size = 0;
public:
Backtrace() = default;
void Initialize();
bool Step();
uintptr_t GetStackPointer() const;
uintptr_t GetReturnAddress() const;
};
}

View file

@ -46,6 +46,7 @@
#include <stratosphere/os/os_sdk_reply_and_receive.hpp>
#include <stratosphere/os/os_thread.hpp>
#include <stratosphere/os/os_sdk_thread_api.hpp>
#include <stratosphere/os/os_sdk_thread_info.hpp>
#include <stratosphere/os/os_message_queue.hpp>
#include <stratosphere/os/os_light_event.hpp>
#include <stratosphere/os/os_light_message_queue.hpp>
@ -55,3 +56,4 @@
#include <stratosphere/os/os_multiple_wait.hpp>
#include <stratosphere/os/os_argument.hpp>
#include <stratosphere/os/os_cache.hpp>
#include <stratosphere/os/os_debug.hpp>

View file

@ -0,0 +1,20 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_debug_types.hpp>
#include <stratosphere/os/os_debug_api.hpp>

View file

@ -0,0 +1,32 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_debug_types.hpp>
#include <stratosphere/os/os_tick.hpp>
namespace ams::os {
void GetCurrentStackInfo(uintptr_t *out_stack, size_t *out_size);
void QueryMemoryInfo(MemoryInfo *out);
Tick GetIdleTickCount();
int GetFreeThreadCount();
}

View file

@ -0,0 +1,31 @@
/*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::os {
struct MemoryInfo {
u64 total_available_memory_size;
size_t total_used_memory_size;
size_t total_memory_heap_size;
size_t allocated_memory_heap_size;
size_t program_size;
size_t total_thread_stack_size;
int thread_count;
};
}

View file

@ -0,0 +1,19 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere/os/os_sdk_thread_info_types.hpp>
#include <stratosphere/os/os_sdk_thread_info_api.hpp>

View file

@ -0,0 +1,25 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere/os/os_sdk_thread_info_types.hpp>
#include <stratosphere/os/os_thread_types.hpp>
namespace ams::os {
void GetThreadStackInfo(uintptr_t *out_stack_top, size_t *out_stack_size, const ThreadType *thread);
}

View file

@ -0,0 +1,25 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere/os/os_common_types.hpp>
namespace ams::os {
enum SdkLastThreadInfoFlag : u32 {
SdkLastThreadInfoFlag_ThreadInSystemCall = (1u << 0),
};
}