BreakPoints.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7. #include "Common/BreakPoints.h"
  8. #include "Common/CommonTypes.h"
  9. #include "Common/DebugInterface.h"
  10. #include "Core/PowerPC/JitCommon/JitBase.h"
  11. #include "Core/PowerPC/JitCommon/JitCache.h"
  12. bool BreakPoints::IsAddressBreakPoint(u32 address) const
  13. {
  14. for (const TBreakPoint& bp : m_BreakPoints)
  15. if (bp.iAddress == address)
  16. return true;
  17. return false;
  18. }
  19. bool BreakPoints::IsTempBreakPoint(u32 address) const
  20. {
  21. for (const TBreakPoint& bp : m_BreakPoints)
  22. if (bp.iAddress == address && bp.bTemporary)
  23. return true;
  24. return false;
  25. }
  26. BreakPoints::TBreakPointsStr BreakPoints::GetStrings() const
  27. {
  28. TBreakPointsStr bps;
  29. for (const TBreakPoint& bp : m_BreakPoints)
  30. {
  31. if (!bp.bTemporary)
  32. {
  33. std::stringstream ss;
  34. ss << std::hex << bp.iAddress << " " << (bp.bOn ? "n" : "");
  35. bps.push_back(ss.str());
  36. }
  37. }
  38. return bps;
  39. }
  40. void BreakPoints::AddFromStrings(const TBreakPointsStr& bpstrs)
  41. {
  42. for (const std::string& bpstr : bpstrs)
  43. {
  44. TBreakPoint bp;
  45. std::stringstream ss;
  46. ss << std::hex << bpstr;
  47. ss >> bp.iAddress;
  48. bp.bOn = bpstr.find("n") != bpstr.npos;
  49. bp.bTemporary = false;
  50. Add(bp);
  51. }
  52. }
  53. void BreakPoints::Add(const TBreakPoint& bp)
  54. {
  55. if (!IsAddressBreakPoint(bp.iAddress))
  56. {
  57. m_BreakPoints.push_back(bp);
  58. if (jit)
  59. jit->GetBlockCache()->InvalidateICache(bp.iAddress, 4, true);
  60. }
  61. }
  62. void BreakPoints::Add(u32 em_address, bool temp)
  63. {
  64. if (!IsAddressBreakPoint(em_address)) // only add new addresses
  65. {
  66. TBreakPoint pt; // breakpoint settings
  67. pt.bOn = true;
  68. pt.bTemporary = temp;
  69. pt.iAddress = em_address;
  70. m_BreakPoints.push_back(pt);
  71. if (jit)
  72. jit->GetBlockCache()->InvalidateICache(em_address, 4, true);
  73. }
  74. }
  75. void BreakPoints::Remove(u32 em_address)
  76. {
  77. for (auto i = m_BreakPoints.begin(); i != m_BreakPoints.end(); ++i)
  78. {
  79. if (i->iAddress == em_address)
  80. {
  81. m_BreakPoints.erase(i);
  82. if (jit)
  83. jit->GetBlockCache()->InvalidateICache(em_address, 4, true);
  84. return;
  85. }
  86. }
  87. }
  88. void BreakPoints::Clear()
  89. {
  90. if (jit)
  91. {
  92. for (const TBreakPoint& bp : m_BreakPoints)
  93. {
  94. jit->GetBlockCache()->InvalidateICache(bp.iAddress, 4, true);
  95. }
  96. }
  97. m_BreakPoints.clear();
  98. }
  99. void BreakPoints::ClearAllTemporary()
  100. {
  101. for (const TBreakPoint& bp : m_BreakPoints)
  102. {
  103. if (bp.bTemporary)
  104. {
  105. if (jit)
  106. jit->GetBlockCache()->InvalidateICache(bp.iAddress, 4, true);
  107. Remove(bp.iAddress);
  108. }
  109. }
  110. }
  111. MemChecks::TMemChecksStr MemChecks::GetStrings() const
  112. {
  113. TMemChecksStr mcs;
  114. for (const TMemCheck& bp : m_MemChecks)
  115. {
  116. std::stringstream mc;
  117. mc << std::hex << bp.StartAddress;
  118. mc << " " << (bp.bRange ? bp.EndAddress : bp.StartAddress) << " " <<
  119. (bp.bRange ? "n" : "") << (bp.OnRead ? "r" : "") <<
  120. (bp.OnWrite ? "w" : "") << (bp.Log ? "l" : "") << (bp.Break ? "p" : "");
  121. mcs.push_back(mc.str());
  122. }
  123. return mcs;
  124. }
  125. void MemChecks::AddFromStrings(const TMemChecksStr& mcstrs)
  126. {
  127. for (const std::string& mcstr : mcstrs)
  128. {
  129. TMemCheck mc;
  130. std::stringstream ss;
  131. ss << std::hex << mcstr;
  132. ss >> mc.StartAddress;
  133. mc.bRange = mcstr.find("n") != mcstr.npos;
  134. mc.OnRead = mcstr.find("r") != mcstr.npos;
  135. mc.OnWrite = mcstr.find("w") != mcstr.npos;
  136. mc.Log = mcstr.find("l") != mcstr.npos;
  137. mc.Break = mcstr.find("p") != mcstr.npos;
  138. if (mc.bRange)
  139. ss >> mc.EndAddress;
  140. else
  141. mc.EndAddress = mc.StartAddress;
  142. Add(mc);
  143. }
  144. }
  145. void MemChecks::Add(const TMemCheck& _rMemoryCheck)
  146. {
  147. bool had_any = HasAny();
  148. if (GetMemCheck(_rMemoryCheck.StartAddress) == nullptr)
  149. m_MemChecks.push_back(_rMemoryCheck);
  150. // If this is the first one, clear the JIT cache so it can switch to
  151. // watchpoint-compatible code.
  152. if (!had_any && jit)
  153. jit->ClearCache();
  154. }
  155. void MemChecks::Remove(u32 _Address)
  156. {
  157. for (auto i = m_MemChecks.begin(); i != m_MemChecks.end(); ++i)
  158. {
  159. if (i->StartAddress == _Address)
  160. {
  161. m_MemChecks.erase(i);
  162. return;
  163. }
  164. }
  165. if (!HasAny() && jit)
  166. jit->ClearCache();
  167. }
  168. TMemCheck *MemChecks::GetMemCheck(u32 address)
  169. {
  170. for (TMemCheck& bp : m_MemChecks)
  171. {
  172. if (bp.bRange)
  173. {
  174. if (address >= bp.StartAddress && address <= bp.EndAddress)
  175. return &(bp);
  176. }
  177. else if (bp.StartAddress == address)
  178. {
  179. return &(bp);
  180. }
  181. }
  182. // none found
  183. return nullptr;
  184. }
  185. bool TMemCheck::Action(DebugInterface *debug_interface, u32 iValue, u32 addr, bool write, int size, u32 pc)
  186. {
  187. if ((write && OnWrite) || (!write && OnRead))
  188. {
  189. if (Log)
  190. {
  191. INFO_LOG(MEMMAP, "CHK %08x (%s) %s%i %0*x at %08x (%s)",
  192. pc, debug_interface->GetDescription(pc).c_str(),
  193. write ? "Write" : "Read", size*8, size*2, iValue, addr,
  194. debug_interface->GetDescription(addr).c_str()
  195. );
  196. }
  197. return true;
  198. }
  199. return false;
  200. }
  201. bool Watches::IsAddressWatch(u32 _iAddress) const
  202. {
  203. for (const TWatch& bp : m_Watches)
  204. if (bp.iAddress == _iAddress)
  205. return true;
  206. return false;
  207. }
  208. Watches::TWatchesStr Watches::GetStrings() const
  209. {
  210. TWatchesStr bps;
  211. for (const TWatch& bp : m_Watches)
  212. {
  213. std::stringstream ss;
  214. ss << std::hex << bp.iAddress << " " << bp.name;
  215. bps.push_back(ss.str());
  216. }
  217. return bps;
  218. }
  219. void Watches::AddFromStrings(const TWatchesStr& bpstrs)
  220. {
  221. for (const std::string& bpstr : bpstrs)
  222. {
  223. TWatch bp;
  224. std::stringstream ss;
  225. ss << std::hex << bpstr;
  226. ss >> bp.iAddress;
  227. ss >> std::ws;
  228. getline(ss, bp.name);
  229. Add(bp);
  230. }
  231. }
  232. void Watches::Add(const TWatch& bp)
  233. {
  234. if (!IsAddressWatch(bp.iAddress))
  235. {
  236. m_Watches.push_back(bp);
  237. }
  238. }
  239. void Watches::Add(u32 em_address)
  240. {
  241. if (!IsAddressWatch(em_address)) // only add new addresses
  242. {
  243. TWatch pt; // breakpoint settings
  244. pt.bOn = true;
  245. pt.iAddress = em_address;
  246. m_Watches.push_back(pt);
  247. }
  248. }
  249. void Watches::Update(int count, u32 em_address)
  250. {
  251. m_Watches.at(count).iAddress = em_address;
  252. }
  253. void Watches::UpdateName(int count, const std::string name)
  254. {
  255. m_Watches.at(count).name = name;
  256. }
  257. void Watches::Remove(u32 em_address)
  258. {
  259. for (auto i = m_Watches.begin(); i != m_Watches.end(); ++i)
  260. {
  261. if (i->iAddress == em_address)
  262. {
  263. m_Watches.erase(i);
  264. return;
  265. }
  266. }
  267. }
  268. void Watches::Clear()
  269. {
  270. m_Watches.clear();
  271. }