2018-12-29 02:55:19 +01:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2019-05-18 03:43:26 +02:00
|
|
|
#include <map>
|
|
|
|
#include <optional>
|
|
|
|
#include "common/bit_field.h"
|
2018-12-29 02:55:19 +01:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/logging/log.h"
|
2018-12-31 02:46:27 +01:00
|
|
|
#include "core/arm/arm_interface.h"
|
2022-04-09 05:53:42 +02:00
|
|
|
#include "core/arm/symbols.h"
|
2019-05-18 03:43:26 +02:00
|
|
|
#include "core/core.h"
|
2022-04-09 05:53:42 +02:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2019-05-26 17:40:41 +02:00
|
|
|
#include "core/loader/loader.h"
|
2018-12-29 02:55:19 +01:00
|
|
|
#include "core/memory.h"
|
|
|
|
|
|
|
|
namespace Core {
|
2019-05-18 03:43:26 +02:00
|
|
|
|
|
|
|
constexpr u64 SEGMENT_BASE = 0x7100000000ull;
|
|
|
|
|
2020-03-20 19:05:47 +01: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-29 06:48:02 +01:00
|
|
|
out.push_back({
|
|
|
|
.module = "",
|
|
|
|
.address = 0,
|
|
|
|
.original_address = lr,
|
|
|
|
.offset = 0,
|
|
|
|
.name = {},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (fp == 0) {
|
2020-03-20 19:05:47 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-10-29 06:48:02 +01:00
|
|
|
|
2020-03-20 19:05:47 +01:00
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
|
2022-04-09 05:53:42 +02:00
|
|
|
std::map<std::string, Symbols::Symbols> symbols;
|
2020-03-20 19:05:47 +01:00
|
|
|
for (const auto& module : modules) {
|
2022-04-09 05:53:42 +02:00
|
|
|
symbols.insert_or_assign(module.second,
|
|
|
|
Symbols::GetSymbols(module.first, system.Memory(),
|
|
|
|
system.CurrentProcess()->Is64BitProcess()));
|
2020-03-20 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
2022-04-09 05:53:42 +02:00
|
|
|
const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset);
|
2020-03-20 19:05:47 +01:00
|
|
|
if (symbol.has_value()) {
|
|
|
|
// TODO(DarkLordZach): Add demangling of symbol names.
|
|
|
|
entry.name = *symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-05-18 03:43:26 +02:00
|
|
|
std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktrace() const {
|
|
|
|
std::vector<BacktraceEntry> out;
|
2019-11-26 22:29:34 +01:00
|
|
|
auto& memory = system.Memory();
|
2019-05-18 03:43:26 +02:00
|
|
|
|
|
|
|
auto fp = GetReg(29);
|
|
|
|
auto lr = GetReg(30);
|
2018-12-31 02:43:15 +01:00
|
|
|
while (true) {
|
2020-10-21 04:07:39 +02:00
|
|
|
out.push_back({"", 0, lr, 0, ""});
|
|
|
|
if (!fp) {
|
2018-12-29 02:55:19 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-11-26 22:29:34 +01:00
|
|
|
lr = memory.Read64(fp + 8) - 4;
|
|
|
|
fp = memory.Read64(fp);
|
2018-12-29 02:55:19 +01:00
|
|
|
}
|
2019-05-18 03:43:26 +02:00
|
|
|
|
2019-05-26 17:40:41 +02:00
|
|
|
std::map<VAddr, std::string> modules;
|
2019-11-26 20:10:49 +01:00
|
|
|
auto& loader{system.GetAppLoader()};
|
2019-05-26 17:40:41 +02:00
|
|
|
if (loader.ReadNSOModules(modules) != Loader::ResultStatus::Success) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-09 05:53:42 +02:00
|
|
|
std::map<std::string, Symbols::Symbols> symbols;
|
2019-05-18 03:43:26 +02:00
|
|
|
for (const auto& module : modules) {
|
2022-04-09 05:53:42 +02:00
|
|
|
symbols.insert_or_assign(module.second,
|
|
|
|
Symbols::GetSymbols(module.first, system.Memory(),
|
|
|
|
system.CurrentProcess()->Is64BitProcess()));
|
2019-05-18 03:43:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& entry : out) {
|
|
|
|
VAddr base = 0;
|
2019-05-26 17:40:41 +02:00
|
|
|
for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) {
|
|
|
|
const auto& module{*iter};
|
2019-05-18 03:43:26 +02: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()) {
|
2022-04-09 05:53:42 +02:00
|
|
|
const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset);
|
2019-05-18 03:43:26 +02:00
|
|
|
if (symbol.has_value()) {
|
|
|
|
// TODO(DarkLordZach): Add demangling of symbol names.
|
|
|
|
entry.name = *symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2018-12-29 02:55:19 +01:00
|
|
|
}
|
2019-05-18 03:43:26 +02: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 17:40:41 +02:00
|
|
|
LOG_ERROR(Core_ARM, "");
|
2019-05-18 03:43:26 +02: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 02:41:30 +01:00
|
|
|
} // namespace Core
|