arm_interface.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/logging/log.h"
  11. #include "core/arm/arm_interface.h"
  12. #include "core/arm/symbols.h"
  13. #include "core/core.h"
  14. #include "core/debugger/debugger.h"
  15. #include "core/hle/kernel/k_process.h"
  16. #include "core/hle/kernel/svc.h"
  17. #include "core/loader/loader.h"
  18. #include "core/memory.h"
  19. #include "core/arm/dynarmic/arm_dynarmic_32.h"
  20. #include "core/arm/dynarmic/arm_dynarmic_64.h"
  21. namespace Core {
  22. constexpr u64 SEGMENT_BASE = 0x7100000000ull;
  23. std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
  24. Core::System& system, const ARM_Interface::ThreadContext32& ctx) {
  25. return ARM_Dynarmic_32::GetBacktraceFromContext(system, ctx);
  26. }
  27. std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
  28. Core::System& system, const ARM_Interface::ThreadContext64& ctx) {
  29. return ARM_Dynarmic_64::GetBacktraceFromContext(system, ctx);
  30. }
  31. void ARM_Interface::SymbolicateBacktrace(Core::System& system, std::vector<BacktraceEntry>& out) {
  32. std::map<VAddr, std::string> modules;
  33. auto& loader{system.GetAppLoader()};
  34. if (loader.ReadNSOModules(modules) != Loader::ResultStatus::Success) {
  35. return;
  36. }
  37. std::map<std::string, Symbols::Symbols> symbols;
  38. for (const auto& module : modules) {
  39. symbols.insert_or_assign(module.second,
  40. Symbols::GetSymbols(module.first, system.Memory(),
  41. system.CurrentProcess()->Is64BitProcess()));
  42. }
  43. for (auto& entry : out) {
  44. VAddr base = 0;
  45. for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) {
  46. const auto& module{*iter};
  47. if (entry.original_address >= module.first) {
  48. entry.module = module.second;
  49. base = module.first;
  50. break;
  51. }
  52. }
  53. entry.offset = entry.original_address - base;
  54. entry.address = SEGMENT_BASE + entry.offset;
  55. if (entry.module.empty()) {
  56. entry.module = "unknown";
  57. }
  58. const auto symbol_set = symbols.find(entry.module);
  59. if (symbol_set != symbols.end()) {
  60. const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset);
  61. if (symbol.has_value()) {
  62. #ifdef _MSC_VER
  63. // TODO(DarkLordZach): Add demangling of symbol names.
  64. entry.name = *symbol;
  65. #else
  66. int status{-1};
  67. char* demangled{abi::__cxa_demangle(symbol->c_str(), nullptr, nullptr, &status)};
  68. if (status == 0 && demangled != nullptr) {
  69. entry.name = demangled;
  70. std::free(demangled);
  71. } else {
  72. entry.name = *symbol;
  73. }
  74. #endif
  75. }
  76. }
  77. }
  78. }
  79. void ARM_Interface::LogBacktrace() const {
  80. const VAddr sp = GetSP();
  81. const VAddr pc = GetPC();
  82. LOG_ERROR(Core_ARM, "Backtrace, sp={:016X}, pc={:016X}", sp, pc);
  83. LOG_ERROR(Core_ARM, "{:20}{:20}{:20}{:20}{}", "Module Name", "Address", "Original Address",
  84. "Offset", "Symbol");
  85. LOG_ERROR(Core_ARM, "");
  86. const auto backtrace = GetBacktrace();
  87. for (const auto& entry : backtrace) {
  88. LOG_ERROR(Core_ARM, "{:20}{:016X} {:016X} {:016X} {}", entry.module, entry.address,
  89. entry.original_address, entry.offset, entry.name);
  90. }
  91. }
  92. void ARM_Interface::Run() {
  93. using Kernel::StepState;
  94. using Kernel::SuspendType;
  95. while (true) {
  96. Kernel::KThread* current_thread{Kernel::GetCurrentThreadPointer(system.Kernel())};
  97. Dynarmic::HaltReason hr{};
  98. // Notify the debugger and go to sleep if a step was performed
  99. // and this thread has been scheduled again.
  100. if (current_thread->GetStepState() == StepState::StepPerformed) {
  101. system.GetDebugger().NotifyThreadStopped(current_thread);
  102. current_thread->RequestSuspend(SuspendType::Debug);
  103. break;
  104. }
  105. // Otherwise, run the thread.
  106. system.EnterDynarmicProfile();
  107. if (current_thread->GetStepState() == StepState::StepPending) {
  108. hr = StepJit();
  109. if (Has(hr, step_thread)) {
  110. current_thread->SetStepState(StepState::StepPerformed);
  111. }
  112. } else {
  113. hr = RunJit();
  114. }
  115. system.ExitDynarmicProfile();
  116. // Notify the debugger and go to sleep if a breakpoint was hit,
  117. // or if the thread is unable to continue for any reason.
  118. if (Has(hr, breakpoint) || Has(hr, no_execute)) {
  119. RewindBreakpointInstruction();
  120. if (system.DebuggerEnabled()) {
  121. system.GetDebugger().NotifyThreadStopped(current_thread);
  122. }
  123. current_thread->RequestSuspend(Kernel::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