2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-12-29 01:55:19 +00:00
|
|
|
|
2019-05-18 01:43:26 +00:00
|
|
|
#include <map>
|
|
|
|
#include <optional>
|
2023-01-13 21:06:13 +00:00
|
|
|
|
2019-05-18 01:43:26 +00:00
|
|
|
#include "common/bit_field.h"
|
2018-12-29 01:55:19 +00:00
|
|
|
#include "common/common_types.h"
|
2023-01-13 21:06:13 +00:00
|
|
|
#include "common/demangle.h"
|
2018-12-29 01:55:19 +00:00
|
|
|
#include "common/logging/log.h"
|
2018-12-31 01:46:27 +00:00
|
|
|
#include "core/arm/arm_interface.h"
|
2022-04-09 03:53:42 +00:00
|
|
|
#include "core/arm/symbols.h"
|
2019-05-18 01:43:26 +00:00
|
|
|
#include "core/core.h"
|
2022-05-30 23:35:01 +00:00
|
|
|
#include "core/debugger/debugger.h"
|
2022-04-09 03:53:42 +00:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2022-05-31 18:37:37 +00:00
|
|
|
#include "core/hle/kernel/svc.h"
|
2019-05-26 15:40:41 +00:00
|
|
|
#include "core/loader/loader.h"
|
2018-12-29 01:55:19 +00:00
|
|
|
#include "core/memory.h"
|
|
|
|
|
2022-04-21 00:17:48 +00:00
|
|
|
#include "core/arm/dynarmic/arm_dynarmic_32.h"
|
|
|
|
#include "core/arm/dynarmic/arm_dynarmic_64.h"
|
|
|
|
|
2018-12-29 01:55:19 +00:00
|
|
|
namespace Core {
|
2019-05-18 01:43:26 +00:00
|
|
|
|
|
|
|
constexpr u64 SEGMENT_BASE = 0x7100000000ull;
|
|
|
|
|
2020-03-20 18:05:47 +00:00
|
|
|
std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
|
2022-04-21 00:17:48 +00:00
|
|
|
Core::System& system, const ARM_Interface::ThreadContext32& ctx) {
|
|
|
|
return ARM_Dynarmic_32::GetBacktraceFromContext(system, ctx);
|
2020-03-20 18:05:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:17:48 +00:00
|
|
|
std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
|
|
|
|
Core::System& system, const ARM_Interface::ThreadContext64& ctx) {
|
|
|
|
return ARM_Dynarmic_64::GetBacktraceFromContext(system, ctx);
|
|
|
|
}
|
2019-05-18 01:43:26 +00:00
|
|
|
|
2022-04-21 00:17:48 +00:00
|
|
|
void ARM_Interface::SymbolicateBacktrace(Core::System& system, std::vector<BacktraceEntry>& out) {
|
2019-05-26 15:40:41 +00:00
|
|
|
std::map<VAddr, std::string> modules;
|
2019-11-26 19:10:49 +00:00
|
|
|
auto& loader{system.GetAppLoader()};
|
2019-05-26 15:40:41 +00:00
|
|
|
if (loader.ReadNSOModules(modules) != Loader::ResultStatus::Success) {
|
2022-04-21 00:17:48 +00:00
|
|
|
return;
|
2019-05-26 15:40:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 03:53:42 +00:00
|
|
|
std::map<std::string, Symbols::Symbols> symbols;
|
2019-05-18 01:43:26 +00:00
|
|
|
for (const auto& module : modules) {
|
2023-02-13 16:21:43 +00:00
|
|
|
symbols.insert_or_assign(
|
|
|
|
module.second, Symbols::GetSymbols(module.first, system.Memory(),
|
|
|
|
system.ApplicationProcess()->Is64BitProcess()));
|
2019-05-18 01:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& entry : out) {
|
|
|
|
VAddr base = 0;
|
2019-05-26 15:40:41 +00:00
|
|
|
for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) {
|
|
|
|
const auto& module{*iter};
|
2019-05-18 01:43:26 +00: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;
|
|
|
|
|
2022-04-21 00:17:48 +00:00
|
|
|
if (entry.module.empty()) {
|
2019-05-18 01:43:26 +00:00
|
|
|
entry.module = "unknown";
|
2022-04-21 00:17:48 +00:00
|
|
|
}
|
2019-05-18 01:43:26 +00:00
|
|
|
|
|
|
|
const auto symbol_set = symbols.find(entry.module);
|
|
|
|
if (symbol_set != symbols.end()) {
|
2022-04-09 03:53:42 +00:00
|
|
|
const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset);
|
2023-01-13 21:06:13 +00:00
|
|
|
if (symbol) {
|
|
|
|
entry.name = Common::DemangleSymbol(*symbol);
|
2019-05-18 01:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 01:55:19 +00:00
|
|
|
}
|
2019-05-18 01:43:26 +00:00
|
|
|
|
|
|
|
void ARM_Interface::LogBacktrace() const {
|
2022-04-21 00:17:48 +00:00
|
|
|
const VAddr sp = GetSP();
|
2019-05-18 01:43:26 +00:00
|
|
|
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 15:40:41 +00:00
|
|
|
LOG_ERROR(Core_ARM, "");
|
2019-05-18 01:43:26 +00: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 18:37:37 +00:00
|
|
|
void ARM_Interface::Run() {
|
|
|
|
using Kernel::StepState;
|
|
|
|
using Kernel::SuspendType;
|
|
|
|
|
|
|
|
while (true) {
|
2022-06-16 14:35:52 +00:00
|
|
|
Kernel::KThread* current_thread{Kernel::GetCurrentThreadPointer(system.Kernel())};
|
2022-05-31 18:37:37 +00:00
|
|
|
Dynarmic::HaltReason hr{};
|
|
|
|
|
|
|
|
// Notify the debugger and go to sleep if a step was performed
|
|
|
|
// and this thread has been scheduled again.
|
|
|
|
if (current_thread->GetStepState() == StepState::StepPerformed) {
|
|
|
|
system.GetDebugger().NotifyThreadStopped(current_thread);
|
|
|
|
current_thread->RequestSuspend(SuspendType::Debug);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, run the thread.
|
2022-06-14 22:19:04 +00:00
|
|
|
system.EnterDynarmicProfile();
|
2022-05-31 18:37:37 +00:00
|
|
|
if (current_thread->GetStepState() == StepState::StepPending) {
|
|
|
|
hr = StepJit();
|
|
|
|
|
|
|
|
if (Has(hr, step_thread)) {
|
|
|
|
current_thread->SetStepState(StepState::StepPerformed);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hr = RunJit();
|
|
|
|
}
|
2022-06-14 22:19:04 +00:00
|
|
|
system.ExitDynarmicProfile();
|
2022-05-31 18:37:37 +00:00
|
|
|
|
2022-09-06 01:19:30 +00:00
|
|
|
// If the thread is scheduled for termination, exit the thread.
|
|
|
|
if (current_thread->HasDpc()) {
|
|
|
|
if (current_thread->IsTerminationRequested()) {
|
|
|
|
current_thread->Exit();
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 00:39:16 +00:00
|
|
|
// Notify the debugger and go to sleep if a breakpoint was hit,
|
|
|
|
// or if the thread is unable to continue for any reason.
|
|
|
|
if (Has(hr, breakpoint) || Has(hr, no_execute)) {
|
2022-12-02 03:48:43 +00:00
|
|
|
if (!Has(hr, no_execute)) {
|
|
|
|
RewindBreakpointInstruction();
|
|
|
|
}
|
2022-06-21 00:39:16 +00:00
|
|
|
if (system.DebuggerEnabled()) {
|
|
|
|
system.GetDebugger().NotifyThreadStopped(current_thread);
|
2022-12-02 03:48:43 +00:00
|
|
|
} else {
|
|
|
|
LogBacktrace();
|
2022-06-21 00:39:16 +00:00
|
|
|
}
|
2022-12-02 03:48:43 +00:00
|
|
|
current_thread->RequestSuspend(SuspendType::Debug);
|
2022-06-06 16:56:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-06-21 00:39:16 +00:00
|
|
|
|
|
|
|
// Notify the debugger and go to sleep if a watchpoint was hit.
|
2022-06-06 16:56:01 +00:00
|
|
|
if (Has(hr, watchpoint)) {
|
2022-06-21 00:39:16 +00:00
|
|
|
if (system.DebuggerEnabled()) {
|
|
|
|
system.GetDebugger().NotifyThreadWatchpoint(current_thread, *HaltedWatchpoint());
|
|
|
|
}
|
2022-06-06 16:56:01 +00:00
|
|
|
current_thread->RequestSuspend(SuspendType::Debug);
|
2022-05-31 18:37:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:52:16 +00:00
|
|
|
// Handle syscalls and scheduling (this may change the current thread/core)
|
2022-05-31 18:37:37 +00:00
|
|
|
if (Has(hr, svc_call)) {
|
|
|
|
Kernel::Svc::Call(system, GetSvcNumber());
|
2022-06-26 22:52:16 +00:00
|
|
|
break;
|
2022-05-31 18:37:37 +00:00
|
|
|
}
|
|
|
|
if (Has(hr, break_loop) || !uses_wall_clock) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 23:35:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 16:56:01 +00:00
|
|
|
void ARM_Interface::LoadWatchpointArray(const WatchpointArray& wp) {
|
|
|
|
watchpoints = ℘
|
|
|
|
}
|
|
|
|
|
|
|
|
const Kernel::DebugWatchpoint* ARM_Interface::MatchingWatchpoint(
|
2023-03-18 01:26:04 +00:00
|
|
|
u64 addr, u64 size, Kernel::DebugWatchpointType access_type) const {
|
2022-06-06 16:56:01 +00:00
|
|
|
if (!watchpoints) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-03-18 01:26:04 +00:00
|
|
|
const u64 start_address{addr};
|
|
|
|
const u64 end_address{addr + size};
|
2022-06-06 16:56:01 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < Core::Hardware::NUM_WATCHPOINTS; i++) {
|
|
|
|
const auto& watch{(*watchpoints)[i]};
|
|
|
|
|
2023-03-18 01:26:04 +00:00
|
|
|
if (end_address <= GetInteger(watch.start_address)) {
|
2022-06-06 16:56:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-03-18 01:26:04 +00:00
|
|
|
if (start_address >= GetInteger(watch.end_address)) {
|
2022-06-06 16:56:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((access_type & watch.type) == Kernel::DebugWatchpointType::None) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &watch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-31 01:41:30 +00:00
|
|
|
} // namespace Core
|