2018-12-29 04:55:19 +03:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2019-05-18 04:43:26 +03:00
|
|
|
#include <map>
|
|
|
|
#include <optional>
|
|
|
|
#include "common/bit_field.h"
|
2018-12-29 04:55:19 +03:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/logging/log.h"
|
2018-12-31 04:46:27 +03:00
|
|
|
#include "core/arm/arm_interface.h"
|
2019-05-18 04:43:26 +03:00
|
|
|
#include "core/core.h"
|
2019-05-26 18:40:41 +03:00
|
|
|
#include "core/loader/loader.h"
|
2018-12-29 04:55:19 +03:00
|
|
|
#include "core/memory.h"
|
|
|
|
|
|
|
|
namespace Core {
|
2019-05-18 04:43:26 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr u64 ELF_DYNAMIC_TAG_NULL = 0;
|
|
|
|
constexpr u64 ELF_DYNAMIC_TAG_STRTAB = 5;
|
|
|
|
constexpr u64 ELF_DYNAMIC_TAG_SYMTAB = 6;
|
|
|
|
constexpr u64 ELF_DYNAMIC_TAG_SYMENT = 11;
|
|
|
|
|
|
|
|
enum class ELFSymbolType : u8 {
|
|
|
|
None = 0,
|
|
|
|
Object = 1,
|
|
|
|
Function = 2,
|
|
|
|
Section = 3,
|
|
|
|
File = 4,
|
|
|
|
Common = 5,
|
|
|
|
TLS = 6,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class ELFSymbolBinding : u8 {
|
|
|
|
Local = 0,
|
|
|
|
Global = 1,
|
|
|
|
Weak = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class ELFSymbolVisibility : u8 {
|
|
|
|
Default = 0,
|
|
|
|
Internal = 1,
|
|
|
|
Hidden = 2,
|
|
|
|
Protected = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ELFSymbol {
|
|
|
|
u32 name_index;
|
|
|
|
union {
|
|
|
|
u8 info;
|
|
|
|
|
|
|
|
BitField<0, 4, ELFSymbolType> type;
|
|
|
|
BitField<4, 4, ELFSymbolBinding> binding;
|
|
|
|
};
|
|
|
|
ELFSymbolVisibility visibility;
|
|
|
|
u16 sh_index;
|
|
|
|
u64 value;
|
|
|
|
u64 size;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(ELFSymbol) == 0x18, "ELFSymbol has incorrect size.");
|
|
|
|
|
|
|
|
using Symbols = std::vector<std::pair<ELFSymbol, std::string>>;
|
|
|
|
|
2020-03-31 22:10:44 +03:00
|
|
|
Symbols GetSymbols(VAddr text_offset, Core::Memory::Memory& memory) {
|
2019-11-27 00:29:34 +03:00
|
|
|
const auto mod_offset = text_offset + memory.Read32(text_offset + 4);
|
2019-05-18 04:43:26 +03:00
|
|
|
|
|
|
|
if (mod_offset < text_offset || (mod_offset & 0b11) != 0 ||
|
2019-11-27 00:29:34 +03:00
|
|
|
memory.Read32(mod_offset) != Common::MakeMagic('M', 'O', 'D', '0')) {
|
2019-05-18 04:43:26 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-11-27 00:29:34 +03:00
|
|
|
const auto dynamic_offset = memory.Read32(mod_offset + 0x4) + mod_offset;
|
2019-05-18 04:43:26 +03:00
|
|
|
|
|
|
|
VAddr string_table_offset{};
|
|
|
|
VAddr symbol_table_offset{};
|
|
|
|
u64 symbol_entry_size{};
|
|
|
|
|
|
|
|
VAddr dynamic_index = dynamic_offset;
|
|
|
|
while (true) {
|
2019-11-27 00:29:34 +03:00
|
|
|
const u64 tag = memory.Read64(dynamic_index);
|
|
|
|
const u64 value = memory.Read64(dynamic_index + 0x8);
|
2019-05-18 04:43:26 +03:00
|
|
|
dynamic_index += 0x10;
|
|
|
|
|
2019-05-26 18:40:41 +03:00
|
|
|
if (tag == ELF_DYNAMIC_TAG_NULL) {
|
2019-05-18 04:43:26 +03:00
|
|
|
break;
|
2019-05-26 18:40:41 +03:00
|
|
|
}
|
2019-05-18 04:43:26 +03:00
|
|
|
|
2019-05-26 18:40:41 +03:00
|
|
|
if (tag == ELF_DYNAMIC_TAG_STRTAB) {
|
2019-05-18 04:43:26 +03:00
|
|
|
string_table_offset = value;
|
2019-05-26 18:40:41 +03:00
|
|
|
} else if (tag == ELF_DYNAMIC_TAG_SYMTAB) {
|
2019-05-18 04:43:26 +03:00
|
|
|
symbol_table_offset = value;
|
2019-05-26 18:40:41 +03:00
|
|
|
} else if (tag == ELF_DYNAMIC_TAG_SYMENT) {
|
2019-05-18 04:43:26 +03:00
|
|
|
symbol_entry_size = value;
|
2019-05-26 18:40:41 +03:00
|
|
|
}
|
2019-05-18 04:43:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (string_table_offset == 0 || symbol_table_offset == 0 || symbol_entry_size == 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto string_table_address = text_offset + string_table_offset;
|
|
|
|
const auto symbol_table_address = text_offset + symbol_table_offset;
|
|
|
|
|
|
|
|
Symbols out;
|
|
|
|
|
|
|
|
VAddr symbol_index = symbol_table_address;
|
|
|
|
while (symbol_index < string_table_address) {
|
|
|
|
ELFSymbol symbol{};
|
2019-11-27 00:29:34 +03:00
|
|
|
memory.ReadBlock(symbol_index, &symbol, sizeof(ELFSymbol));
|
2019-05-18 04:43:26 +03:00
|
|
|
|
|
|
|
VAddr string_offset = string_table_address + symbol.name_index;
|
|
|
|
std::string name;
|
2019-11-27 00:29:34 +03:00
|
|
|
for (u8 c = memory.Read8(string_offset); c != 0; c = memory.Read8(++string_offset)) {
|
2019-05-18 04:43:26 +03:00
|
|
|
name += static_cast<char>(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol_index += symbol_entry_size;
|
|
|
|
out.push_back({symbol, name});
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> GetSymbolName(const Symbols& symbols, VAddr func_address) {
|
|
|
|
const auto iter =
|
|
|
|
std::find_if(symbols.begin(), symbols.end(), [func_address](const auto& pair) {
|
2020-04-15 22:59:23 +03:00
|
|
|
const auto& symbol = pair.first;
|
2019-05-18 04:43:26 +03:00
|
|
|
const auto end_address = symbol.value + symbol.size;
|
|
|
|
return func_address >= symbol.value && func_address < end_address;
|
|
|
|
});
|
|
|
|
|
2019-05-26 18:40:41 +03:00
|
|
|
if (iter == symbols.end()) {
|
2019-05-18 04:43:26 +03:00
|
|
|
return std::nullopt;
|
2019-05-26 18:40:41 +03:00
|
|
|
}
|
|
|
|
|
2019-05-18 04:43:26 +03:00
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
constexpr u64 SEGMENT_BASE = 0x7100000000ull;
|
|
|
|
|
2020-03-20 21:05:47 +03:00
|
|
|
std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
|
|
|
|
System& system, const ThreadContext64& ctx) {
|
|
|
|
std::vector<BacktraceEntry> out;
|
|
|
|
auto& memory = system.Memory();
|
|
|
|
|
|
|
|
auto fp = ctx.cpu_registers[29];
|
|
|
|
auto lr = ctx.cpu_registers[30];
|
|
|
|
while (true) {
|
2020-10-21 05:07:39 +03:00
|
|
|
out.push_back({"", 0, lr, 0});
|
|
|
|
if (!fp) {
|
2020-03-20 21:05:47 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
lr = memory.Read64(fp + 8) - 4;
|
|
|
|
fp = memory.Read64(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<VAddr, std::string> modules;
|
|
|
|
auto& loader{system.GetAppLoader()};
|
|
|
|
if (loader.ReadNSOModules(modules) != Loader::ResultStatus::Success) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, Symbols> symbols;
|
|
|
|
for (const auto& module : modules) {
|
|
|
|
symbols.insert_or_assign(module.second, GetSymbols(module.first, memory));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& entry : out) {
|
|
|
|
VAddr base = 0;
|
|
|
|
for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) {
|
|
|
|
const auto& module{*iter};
|
|
|
|
if (entry.original_address >= module.first) {
|
|
|
|
entry.module = module.second;
|
|
|
|
base = module.first;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.offset = entry.original_address - base;
|
|
|
|
entry.address = SEGMENT_BASE + entry.offset;
|
|
|
|
|
|
|
|
if (entry.module.empty())
|
|
|
|
entry.module = "unknown";
|
|
|
|
|
|
|
|
const auto symbol_set = symbols.find(entry.module);
|
|
|
|
if (symbol_set != symbols.end()) {
|
|
|
|
const auto symbol = GetSymbolName(symbol_set->second, entry.offset);
|
|
|
|
if (symbol.has_value()) {
|
|
|
|
// TODO(DarkLordZach): Add demangling of symbol names.
|
|
|
|
entry.name = *symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-05-18 04:43:26 +03:00
|
|
|
std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktrace() const {
|
|
|
|
std::vector<BacktraceEntry> out;
|
2019-11-27 00:29:34 +03:00
|
|
|
auto& memory = system.Memory();
|
2019-05-18 04:43:26 +03:00
|
|
|
|
|
|
|
auto fp = GetReg(29);
|
|
|
|
auto lr = GetReg(30);
|
2018-12-31 04:43:15 +03:00
|
|
|
while (true) {
|
2020-10-21 05:07:39 +03:00
|
|
|
out.push_back({"", 0, lr, 0, ""});
|
|
|
|
if (!fp) {
|
2018-12-29 04:55:19 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-11-27 00:29:34 +03:00
|
|
|
lr = memory.Read64(fp + 8) - 4;
|
|
|
|
fp = memory.Read64(fp);
|
2018-12-29 04:55:19 +03:00
|
|
|
}
|
2019-05-18 04:43:26 +03:00
|
|
|
|
2019-05-26 18:40:41 +03:00
|
|
|
std::map<VAddr, std::string> modules;
|
2019-11-26 22:10:49 +03:00
|
|
|
auto& loader{system.GetAppLoader()};
|
2019-05-26 18:40:41 +03:00
|
|
|
if (loader.ReadNSOModules(modules) != Loader::ResultStatus::Success) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-05-18 04:43:26 +03:00
|
|
|
std::map<std::string, Symbols> symbols;
|
|
|
|
for (const auto& module : modules) {
|
2019-11-27 00:29:34 +03:00
|
|
|
symbols.insert_or_assign(module.second, GetSymbols(module.first, memory));
|
2019-05-18 04:43:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& entry : out) {
|
|
|
|
VAddr base = 0;
|
2019-05-26 18:40:41 +03:00
|
|
|
for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) {
|
|
|
|
const auto& module{*iter};
|
2019-05-18 04:43:26 +03:00
|
|
|
if (entry.original_address >= module.first) {
|
|
|
|
entry.module = module.second;
|
|
|
|
base = module.first;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.offset = entry.original_address - base;
|
|
|
|
entry.address = SEGMENT_BASE + entry.offset;
|
|
|
|
|
|
|
|
if (entry.module.empty())
|
|
|
|
entry.module = "unknown";
|
|
|
|
|
|
|
|
const auto symbol_set = symbols.find(entry.module);
|
|
|
|
if (symbol_set != symbols.end()) {
|
|
|
|
const auto symbol = GetSymbolName(symbol_set->second, entry.offset);
|
|
|
|
if (symbol.has_value()) {
|
|
|
|
// TODO(DarkLordZach): Add demangling of symbol names.
|
|
|
|
entry.name = *symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2018-12-29 04:55:19 +03:00
|
|
|
}
|
2019-05-18 04:43:26 +03:00
|
|
|
|
|
|
|
void ARM_Interface::LogBacktrace() const {
|
|
|
|
const VAddr sp = GetReg(13);
|
|
|
|
const VAddr pc = GetPC();
|
|
|
|
LOG_ERROR(Core_ARM, "Backtrace, sp={:016X}, pc={:016X}", sp, pc);
|
|
|
|
LOG_ERROR(Core_ARM, "{:20}{:20}{:20}{:20}{}", "Module Name", "Address", "Original Address",
|
|
|
|
"Offset", "Symbol");
|
2019-05-26 18:40:41 +03:00
|
|
|
LOG_ERROR(Core_ARM, "");
|
2019-05-18 04:43:26 +03:00
|
|
|
|
|
|
|
const auto backtrace = GetBacktrace();
|
|
|
|
for (const auto& entry : backtrace) {
|
|
|
|
LOG_ERROR(Core_ARM, "{:20}{:016X} {:016X} {:016X} {}", entry.module, entry.address,
|
|
|
|
entry.original_address, entry.offset, entry.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 04:41:30 +03:00
|
|
|
} // namespace Core
|