arm_interface.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <map>
  4. #include <optional>
  5. #include "common/bit_field.h"
  6. #include "common/common_types.h"
  7. #include "common/logging/log.h"
  8. #include "core/arm/arm_interface.h"
  9. #include "core/arm/symbols.h"
  10. #include "core/core.h"
  11. #include "core/debugger/debugger.h"
  12. #include "core/hle/kernel/k_process.h"
  13. #include "core/hle/kernel/svc.h"
  14. #include "core/loader/loader.h"
  15. #include "core/memory.h"
  16. #include "core/arm/dynarmic/arm_dynarmic_32.h"
  17. #include "core/arm/dynarmic/arm_dynarmic_64.h"
  18. namespace Core {
  19. constexpr u64 SEGMENT_BASE = 0x7100000000ull;
  20. std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
  21. Core::System& system, const ARM_Interface::ThreadContext32& ctx) {
  22. return ARM_Dynarmic_32::GetBacktraceFromContext(system, ctx);
  23. }
  24. std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
  25. Core::System& system, const ARM_Interface::ThreadContext64& ctx) {
  26. return ARM_Dynarmic_64::GetBacktraceFromContext(system, ctx);
  27. }
  28. void ARM_Interface::SymbolicateBacktrace(Core::System& system, std::vector<BacktraceEntry>& out) {
  29. std::map<VAddr, std::string> modules;
  30. auto& loader{system.GetAppLoader()};
  31. if (loader.ReadNSOModules(modules) != Loader::ResultStatus::Success) {
  32. return;
  33. }
  34. std::map<std::string, Symbols::Symbols> symbols;
  35. for (const auto& module : modules) {
  36. symbols.insert_or_assign(module.second,
  37. Symbols::GetSymbols(module.first, system.Memory(),
  38. system.CurrentProcess()->Is64BitProcess()));
  39. }
  40. for (auto& entry : out) {
  41. VAddr base = 0;
  42. for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) {
  43. const auto& module{*iter};
  44. if (entry.original_address >= module.first) {
  45. entry.module = module.second;
  46. base = module.first;
  47. break;
  48. }
  49. }
  50. entry.offset = entry.original_address - base;
  51. entry.address = SEGMENT_BASE + entry.offset;
  52. if (entry.module.empty()) {
  53. entry.module = "unknown";
  54. }
  55. const auto symbol_set = symbols.find(entry.module);
  56. if (symbol_set != symbols.end()) {
  57. const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset);
  58. if (symbol.has_value()) {
  59. // TODO(DarkLordZach): Add demangling of symbol names.
  60. entry.name = *symbol;
  61. }
  62. }
  63. }
  64. }
  65. void ARM_Interface::LogBacktrace() const {
  66. const VAddr sp = GetSP();
  67. const VAddr pc = GetPC();
  68. LOG_ERROR(Core_ARM, "Backtrace, sp={:016X}, pc={:016X}", sp, pc);
  69. LOG_ERROR(Core_ARM, "{:20}{:20}{:20}{:20}{}", "Module Name", "Address", "Original Address",
  70. "Offset", "Symbol");
  71. LOG_ERROR(Core_ARM, "");
  72. const auto backtrace = GetBacktrace();
  73. for (const auto& entry : backtrace) {
  74. LOG_ERROR(Core_ARM, "{:20}{:016X} {:016X} {:016X} {}", entry.module, entry.address,
  75. entry.original_address, entry.offset, entry.name);
  76. }
  77. }
  78. void ARM_Interface::Run() {
  79. using Kernel::StepState;
  80. using Kernel::SuspendType;
  81. while (true) {
  82. Kernel::KThread* current_thread{Kernel::GetCurrentThreadPointer(system.Kernel())};
  83. Dynarmic::HaltReason hr{};
  84. // Notify the debugger and go to sleep if a step was performed
  85. // and this thread has been scheduled again.
  86. if (current_thread->GetStepState() == StepState::StepPerformed) {
  87. system.GetDebugger().NotifyThreadStopped(current_thread);
  88. current_thread->RequestSuspend(SuspendType::Debug);
  89. break;
  90. }
  91. // Otherwise, run the thread.
  92. system.EnterDynarmicProfile();
  93. if (current_thread->GetStepState() == StepState::StepPending) {
  94. hr = StepJit();
  95. if (Has(hr, step_thread)) {
  96. current_thread->SetStepState(StepState::StepPerformed);
  97. }
  98. } else {
  99. hr = RunJit();
  100. }
  101. system.ExitDynarmicProfile();
  102. // Notify the debugger and go to sleep if a breakpoint was hit.
  103. if (Has(hr, breakpoint)) {
  104. RewindBreakpointInstruction();
  105. system.GetDebugger().NotifyThreadStopped(current_thread);
  106. current_thread->RequestSuspend(SuspendType::Debug);
  107. break;
  108. }
  109. if (Has(hr, watchpoint)) {
  110. RewindBreakpointInstruction();
  111. system.GetDebugger().NotifyThreadWatchpoint(current_thread, *HaltedWatchpoint());
  112. current_thread->RequestSuspend(SuspendType::Debug);
  113. break;
  114. }
  115. // Handle syscalls and scheduling (this may change the current thread)
  116. if (Has(hr, svc_call)) {
  117. Kernel::Svc::Call(system, GetSvcNumber());
  118. }
  119. if (Has(hr, break_loop) || !uses_wall_clock) {
  120. break;
  121. }
  122. }
  123. }
  124. void ARM_Interface::LoadWatchpointArray(const WatchpointArray& wp) {
  125. watchpoints = &wp;
  126. }
  127. const Kernel::DebugWatchpoint* ARM_Interface::MatchingWatchpoint(
  128. VAddr addr, u64 size, Kernel::DebugWatchpointType access_type) const {
  129. if (!watchpoints) {
  130. return nullptr;
  131. }
  132. const VAddr start_address{addr};
  133. const VAddr end_address{addr + size};
  134. for (size_t i = 0; i < Core::Hardware::NUM_WATCHPOINTS; i++) {
  135. const auto& watch{(*watchpoints)[i]};
  136. if (end_address <= watch.start_address) {
  137. continue;
  138. }
  139. if (start_address >= watch.end_address) {
  140. continue;
  141. }
  142. if ((access_type & watch.type) == Kernel::DebugWatchpointType::None) {
  143. continue;
  144. }
  145. return &watch;
  146. }
  147. return nullptr;
  148. }
  149. } // namespace Core