arm_interface.cpp 6.4 KB

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