2019-07-12 13:31:00 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-07-12 13:31:00 +01: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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <stratosphere.hpp>
|
2020-03-08 09:45:12 +00:00
|
|
|
#include "creport_scoped_file.hpp"
|
2019-07-12 13:31:00 +01:00
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::creport {
|
2019-07-12 13:31:00 +01:00
|
|
|
|
|
|
|
/* Forward declare ModuleList class. */
|
|
|
|
class ModuleList;
|
|
|
|
|
2021-01-19 01:50:40 +00:00
|
|
|
static constexpr size_t ThreadCountMax = 0x60;
|
|
|
|
|
|
|
|
template<size_t MaxThreadCount>
|
|
|
|
class ThreadTlsMapImpl {
|
|
|
|
private:
|
|
|
|
std::pair<u64, u64> m_map[MaxThreadCount];
|
|
|
|
size_t m_index;
|
|
|
|
public:
|
|
|
|
constexpr ThreadTlsMapImpl() : m_map(), m_index(0) { /* ... */ }
|
|
|
|
|
|
|
|
constexpr void ResetThreadTlsMap() {
|
|
|
|
m_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void SetThreadTls(u64 thread_id, u64 tls) {
|
|
|
|
if (m_index < util::size(m_map)) {
|
|
|
|
m_map[m_index++] = std::make_pair(thread_id, tls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool GetThreadTls(u64 *out, u64 thread_id) const {
|
|
|
|
for (size_t i = 0; i < m_index; ++i) {
|
|
|
|
if (m_map[i].first == thread_id) {
|
|
|
|
*out = m_map[i].second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using ThreadTlsMap = ThreadTlsMapImpl<ThreadCountMax>;
|
|
|
|
|
2019-07-12 13:31:00 +01:00
|
|
|
class ThreadInfo {
|
|
|
|
private:
|
|
|
|
static constexpr size_t StackTraceSizeMax = 0x20;
|
|
|
|
static constexpr size_t NameLengthMax = 0x20;
|
|
|
|
private:
|
2021-10-10 08:14:06 +01:00
|
|
|
svc::ThreadContext m_context = {};
|
|
|
|
u64 m_thread_id = 0;
|
|
|
|
u64 m_stack_top = 0;
|
|
|
|
u64 m_stack_bottom = 0;
|
|
|
|
u64 m_stack_trace[StackTraceSizeMax] = {};
|
|
|
|
size_t m_stack_trace_size = 0;
|
|
|
|
u64 m_tls_address = 0;
|
|
|
|
u8 m_tls[0x100] = {};
|
|
|
|
u64 m_stack_dump_base = 0;
|
|
|
|
u8 m_stack_dump[0x100] = {};
|
|
|
|
char m_name[NameLengthMax + 1] = {};
|
|
|
|
ModuleList *m_module_list = nullptr;
|
2019-07-12 13:31:00 +01:00
|
|
|
public:
|
|
|
|
u64 GetGeneralPurposeRegister(size_t i) const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_context.r[i];
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
2019-07-13 02:18:31 +01:00
|
|
|
|
2019-07-12 13:31:00 +01:00
|
|
|
u64 GetPC() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_context.pc;
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetLR() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_context.lr;
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetFP() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_context.fp;
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetSP() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_context.sp;
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetThreadId() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_thread_id;
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetStackTraceSize() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_stack_trace_size;
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetStackTrace(size_t i) const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_stack_trace[i];
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetModuleList(ModuleList *ml) {
|
2021-10-10 08:14:06 +01:00
|
|
|
m_module_list = ml;
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 22:54:13 +01:00
|
|
|
bool ReadFromProcess(os::NativeHandle debug_handle, ThreadTlsMap &tls_map, u64 thread_id, bool is_64_bit);
|
2020-03-08 09:45:12 +00:00
|
|
|
void SaveToFile(ScopedFile &file);
|
|
|
|
void DumpBinary(ScopedFile &file);
|
2019-07-12 13:31:00 +01:00
|
|
|
private:
|
2021-10-04 22:54:13 +01:00
|
|
|
void TryGetStackInfo(os::NativeHandle debug_handle);
|
2019-07-12 13:31:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadList {
|
|
|
|
private:
|
2021-10-10 08:14:06 +01:00
|
|
|
size_t m_thread_count = 0;
|
|
|
|
ThreadInfo m_threads[ThreadCountMax];
|
2019-07-12 13:31:00 +01:00
|
|
|
public:
|
|
|
|
size_t GetThreadCount() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_thread_count;
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const ThreadInfo &GetThreadInfo(size_t i) const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_threads[i];
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetModuleList(ModuleList *ml) {
|
2021-10-10 08:14:06 +01:00
|
|
|
for (size_t i = 0; i < m_thread_count; i++) {
|
|
|
|
m_threads[i].SetModuleList(ml);
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 22:54:13 +01:00
|
|
|
void ReadFromProcess(os::NativeHandle debug_handle, ThreadTlsMap &tls_map, bool is_64_bit);
|
2020-03-08 09:45:12 +00:00
|
|
|
void SaveToFile(ScopedFile &file);
|
|
|
|
void DumpBinary(ScopedFile &file, u64 crashed_thread_id);
|
2019-07-12 13:31:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|