2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-23 14:18:14 -05:00
|
|
|
#include "Core/PowerPC/BreakPoints.h"
|
|
|
|
|
2017-01-11 16:34:38 -05:00
|
|
|
#include <algorithm>
|
2017-03-02 20:07:24 -05:00
|
|
|
#include <cstddef>
|
2011-02-25 09:35:56 +00:00
|
|
|
#include <sstream>
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-09-07 20:06:58 -05:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Common/DebugInterface.h"
|
2018-03-19 02:55:54 -04:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-02-24 21:10:22 -05:00
|
|
|
#include "Core/Core.h"
|
2020-12-16 15:40:20 -08:00
|
|
|
#include "Core/PowerPC/Expression.h"
|
2018-03-19 02:55:54 -04:00
|
|
|
#include "Core/PowerPC/JitInterface.h"
|
2018-05-17 17:09:55 -04:00
|
|
|
#include "Core/PowerPC/MMU.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
|
2015-04-15 00:38:21 -04:00
|
|
|
bool BreakPoints::IsAddressBreakPoint(u32 address) const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2017-01-11 16:34:38 -05:00
|
|
|
return std::any_of(m_breakpoints.begin(), m_breakpoints.end(),
|
|
|
|
[address](const auto& bp) { return bp.address == address; });
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2021-02-28 08:04:50 +00:00
|
|
|
bool BreakPoints::IsBreakPointEnable(u32 address) const
|
|
|
|
{
|
|
|
|
return std::any_of(m_breakpoints.begin(), m_breakpoints.end(),
|
|
|
|
[address](const auto& bp) { return bp.is_enabled && bp.address == address; });
|
|
|
|
}
|
|
|
|
|
2015-04-15 00:38:21 -04:00
|
|
|
bool BreakPoints::IsTempBreakPoint(u32 address) const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2017-01-11 16:34:38 -05:00
|
|
|
return std::any_of(m_breakpoints.begin(), m_breakpoints.end(), [address](const auto& bp) {
|
|
|
|
return bp.address == address && bp.is_temporary;
|
|
|
|
});
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2020-12-16 15:40:20 -08:00
|
|
|
const TBreakPoint* BreakPoints::GetBreakpoint(u32 address) const
|
2020-07-06 18:16:32 -04:00
|
|
|
{
|
2020-12-16 15:40:20 -08:00
|
|
|
auto bp = std::find_if(m_breakpoints.begin(), m_breakpoints.end(), [address](const auto& bp) {
|
|
|
|
return bp.is_enabled && bp.address == address;
|
2020-07-06 18:16:32 -04:00
|
|
|
});
|
|
|
|
|
2020-12-16 15:40:20 -08:00
|
|
|
if (bp == m_breakpoints.end() || !EvaluateCondition(bp->condition))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return &*bp;
|
2020-07-06 18:16:32 -04:00
|
|
|
}
|
|
|
|
|
2011-02-25 09:35:56 +00:00
|
|
|
BreakPoints::TBreakPointsStr BreakPoints::GetStrings() const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2017-01-11 08:27:45 -05:00
|
|
|
TBreakPointsStr bp_strings;
|
|
|
|
for (const TBreakPoint& bp : m_breakpoints)
|
2011-02-25 09:35:56 +00:00
|
|
|
{
|
2017-01-11 08:27:45 -05:00
|
|
|
if (!bp.is_temporary)
|
2011-02-25 09:35:56 +00:00
|
|
|
{
|
2019-09-14 16:40:34 -04:00
|
|
|
std::ostringstream ss;
|
2021-02-28 08:04:50 +00:00
|
|
|
ss.imbue(std::locale::classic());
|
2020-12-16 15:40:20 -08:00
|
|
|
ss << fmt::format("${:08x} ", bp.address);
|
|
|
|
if (bp.is_enabled)
|
|
|
|
ss << "n";
|
|
|
|
if (bp.log_on_hit)
|
|
|
|
ss << "l";
|
|
|
|
if (bp.break_on_hit)
|
|
|
|
ss << "b";
|
|
|
|
if (bp.condition)
|
|
|
|
ss << "c " << bp.condition->GetText();
|
|
|
|
bp_strings.emplace_back(ss.str());
|
2011-02-25 09:35:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-01-11 08:27:45 -05:00
|
|
|
return bp_strings;
|
2011-02-25 09:35:56 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-01-11 08:27:45 -05:00
|
|
|
void BreakPoints::AddFromStrings(const TBreakPointsStr& bp_strings)
|
2011-02-25 09:35:56 +00:00
|
|
|
{
|
2017-01-11 08:27:45 -05:00
|
|
|
for (const std::string& bp_string : bp_strings)
|
2011-02-25 09:35:56 +00:00
|
|
|
{
|
|
|
|
TBreakPoint bp;
|
2021-01-19 15:03:30 -05:00
|
|
|
std::string flags;
|
|
|
|
std::istringstream iss(bp_string);
|
2021-02-28 08:04:50 +00:00
|
|
|
iss.imbue(std::locale::classic());
|
|
|
|
|
2020-12-16 15:40:20 -08:00
|
|
|
if (iss.peek() == '$')
|
|
|
|
iss.ignore();
|
|
|
|
|
2021-01-19 15:03:30 -05:00
|
|
|
iss >> std::hex >> bp.address;
|
|
|
|
iss >> flags;
|
|
|
|
bp.is_enabled = flags.find('n') != flags.npos;
|
|
|
|
bp.log_on_hit = flags.find('l') != flags.npos;
|
|
|
|
bp.break_on_hit = flags.find('b') != flags.npos;
|
2020-12-16 15:40:20 -08:00
|
|
|
if (flags.find('c') != std::string::npos)
|
|
|
|
{
|
|
|
|
iss >> std::ws;
|
|
|
|
std::string condition;
|
|
|
|
std::getline(iss, condition);
|
|
|
|
bp.condition = Expression::TryParse(condition);
|
|
|
|
}
|
2017-01-11 08:27:45 -05:00
|
|
|
bp.is_temporary = false;
|
2020-12-16 15:40:20 -08:00
|
|
|
Add(std::move(bp));
|
2011-02-25 09:35:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:40:20 -08:00
|
|
|
void BreakPoints::Add(TBreakPoint bp)
|
2011-02-25 09:35:56 +00:00
|
|
|
{
|
2018-03-19 02:51:59 -04:00
|
|
|
if (IsAddressBreakPoint(bp.address))
|
|
|
|
return;
|
|
|
|
|
2018-03-19 02:55:54 -04:00
|
|
|
JitInterface::InvalidateICache(bp.address, 4, true);
|
2020-12-16 15:40:20 -08:00
|
|
|
|
|
|
|
m_breakpoints.emplace_back(std::move(bp));
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-01-11 08:27:45 -05:00
|
|
|
void BreakPoints::Add(u32 address, bool temp)
|
2020-07-06 18:16:32 -04:00
|
|
|
{
|
2020-12-16 15:40:20 -08:00
|
|
|
BreakPoints::Add(address, temp, true, false, std::nullopt);
|
2020-07-06 18:16:32 -04:00
|
|
|
}
|
|
|
|
|
2020-12-16 15:40:20 -08:00
|
|
|
void BreakPoints::Add(u32 address, bool temp, bool break_on_hit, bool log_on_hit,
|
|
|
|
std::optional<Expression> condition)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2018-03-19 02:51:59 -04:00
|
|
|
// Only add new addresses
|
|
|
|
if (IsAddressBreakPoint(address))
|
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-03-19 02:51:59 -04:00
|
|
|
TBreakPoint bp; // breakpoint settings
|
|
|
|
bp.is_enabled = true;
|
|
|
|
bp.is_temporary = temp;
|
2020-07-06 18:16:32 -04:00
|
|
|
bp.break_on_hit = break_on_hit;
|
|
|
|
bp.log_on_hit = log_on_hit;
|
2018-03-19 02:51:59 -04:00
|
|
|
bp.address = address;
|
2020-12-16 15:40:20 -08:00
|
|
|
bp.condition = std::move(condition);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-12-16 15:40:20 -08:00
|
|
|
m_breakpoints.emplace_back(std::move(bp));
|
2018-03-19 02:51:59 -04:00
|
|
|
|
2018-03-19 02:55:54 -04:00
|
|
|
JitInterface::InvalidateICache(address, 4, true);
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2021-02-28 08:04:50 +00:00
|
|
|
bool BreakPoints::ToggleBreakPoint(u32 address)
|
|
|
|
{
|
|
|
|
auto iter = std::find_if(m_breakpoints.begin(), m_breakpoints.end(),
|
|
|
|
[address](const auto& bp) { return bp.address == address; });
|
|
|
|
|
|
|
|
if (iter == m_breakpoints.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
iter->is_enabled = !iter->is_enabled;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-11 08:27:45 -05:00
|
|
|
void BreakPoints::Remove(u32 address)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2018-03-19 02:36:38 -04:00
|
|
|
const auto iter = std::find_if(m_breakpoints.begin(), m_breakpoints.end(),
|
|
|
|
[address](const auto& bp) { return bp.address == address; });
|
|
|
|
|
|
|
|
if (iter == m_breakpoints.cend())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_breakpoints.erase(iter);
|
2018-03-19 02:55:54 -04:00
|
|
|
JitInterface::InvalidateICache(address, 4, true);
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2012-03-15 21:48:19 +11:00
|
|
|
void BreakPoints::Clear()
|
|
|
|
{
|
2018-03-19 02:55:54 -04:00
|
|
|
for (const TBreakPoint& bp : m_breakpoints)
|
2012-03-15 21:48:19 +11:00
|
|
|
{
|
2018-03-19 02:55:54 -04:00
|
|
|
JitInterface::InvalidateICache(bp.address, 4, true);
|
2012-03-15 21:48:19 +11:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-01-11 08:27:45 -05:00
|
|
|
m_breakpoints.clear();
|
2012-03-15 21:48:19 +11:00
|
|
|
}
|
2011-02-25 09:35:56 +00:00
|
|
|
|
2014-10-18 11:02:26 +11:00
|
|
|
void BreakPoints::ClearAllTemporary()
|
|
|
|
{
|
2017-01-11 08:27:45 -05:00
|
|
|
auto bp = m_breakpoints.begin();
|
|
|
|
while (bp != m_breakpoints.end())
|
2014-10-18 11:02:26 +11:00
|
|
|
{
|
2017-01-11 08:27:45 -05:00
|
|
|
if (bp->is_temporary)
|
2014-10-18 11:02:26 +11:00
|
|
|
{
|
2018-03-19 02:55:54 -04:00
|
|
|
JitInterface::InvalidateICache(bp->address, 4, true);
|
2017-01-11 08:27:45 -05:00
|
|
|
bp = m_breakpoints.erase(bp);
|
2016-10-15 18:51:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++bp;
|
2014-10-18 11:02:26 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-25 09:35:56 +00:00
|
|
|
MemChecks::TMemChecksStr MemChecks::GetStrings() const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2017-01-11 08:27:45 -05:00
|
|
|
TMemChecksStr mc_strings;
|
|
|
|
for (const TMemCheck& mc : m_mem_checks)
|
2011-02-25 09:35:56 +00:00
|
|
|
{
|
2019-09-14 16:40:34 -04:00
|
|
|
std::ostringstream ss;
|
2021-02-28 08:04:50 +00:00
|
|
|
ss.imbue(std::locale::classic());
|
|
|
|
|
|
|
|
ss << std::hex << mc.start_address << " " << mc.end_address << " " << (mc.is_enabled ? "n" : "")
|
|
|
|
<< (mc.is_break_on_read ? "r" : "") << (mc.is_break_on_write ? "w" : "")
|
|
|
|
<< (mc.log_on_hit ? "l" : "") << (mc.break_on_hit ? "b" : "");
|
2017-01-11 08:27:45 -05:00
|
|
|
mc_strings.push_back(ss.str());
|
2011-02-25 09:35:56 +00:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-01-11 08:27:45 -05:00
|
|
|
return mc_strings;
|
2008-10-18 02:21:59 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-01-11 08:27:45 -05:00
|
|
|
void MemChecks::AddFromStrings(const TMemChecksStr& mc_strings)
|
2008-07-17 21:46:34 +00:00
|
|
|
{
|
2017-01-11 08:27:45 -05:00
|
|
|
for (const std::string& mc_string : mc_strings)
|
2011-02-25 09:35:56 +00:00
|
|
|
{
|
|
|
|
TMemCheck mc;
|
2021-02-28 08:04:50 +00:00
|
|
|
std::istringstream iss(mc_string);
|
|
|
|
iss.imbue(std::locale::classic());
|
|
|
|
|
|
|
|
std::string flags;
|
|
|
|
iss >> std::hex >> mc.start_address >> mc.end_address >> flags;
|
|
|
|
|
|
|
|
mc.is_ranged = mc.start_address != mc.end_address;
|
|
|
|
mc.is_enabled = flags.find('n') != flags.npos;
|
|
|
|
mc.is_break_on_read = flags.find('r') != flags.npos;
|
|
|
|
mc.is_break_on_write = flags.find('w') != flags.npos;
|
|
|
|
mc.log_on_hit = flags.find('l') != flags.npos;
|
|
|
|
mc.break_on_hit = flags.find('b') != flags.npos;
|
|
|
|
|
2011-02-25 09:35:56 +00:00
|
|
|
Add(mc);
|
|
|
|
}
|
2008-12-20 14:00:33 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 08:27:45 -05:00
|
|
|
void MemChecks::Add(const TMemCheck& memory_check)
|
2008-12-20 14:00:33 +00:00
|
|
|
{
|
2018-03-19 02:51:59 -04:00
|
|
|
if (GetMemCheck(memory_check.start_address) != nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool had_any = HasAny();
|
|
|
|
Core::RunAsCPUThread([&] {
|
|
|
|
m_mem_checks.push_back(memory_check);
|
|
|
|
// If this is the first one, clear the JIT cache so it can switch to
|
|
|
|
// watchpoint-compatible code.
|
2018-03-19 02:55:54 -04:00
|
|
|
if (!had_any)
|
|
|
|
JitInterface::ClearCache();
|
2018-03-19 02:51:59 -04:00
|
|
|
PowerPC::DBATUpdated();
|
|
|
|
});
|
2008-12-20 14:00:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 08:04:50 +00:00
|
|
|
bool MemChecks::ToggleBreakPoint(u32 address)
|
|
|
|
{
|
|
|
|
auto iter = std::find_if(m_mem_checks.begin(), m_mem_checks.end(),
|
|
|
|
[address](const auto& bp) { return bp.start_address == address; });
|
|
|
|
|
|
|
|
if (iter == m_mem_checks.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
iter->is_enabled = !iter->is_enabled;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-11 08:27:45 -05:00
|
|
|
void MemChecks::Remove(u32 address)
|
2008-12-20 14:00:33 +00:00
|
|
|
{
|
2018-03-19 02:36:38 -04:00
|
|
|
const auto iter =
|
|
|
|
std::find_if(m_mem_checks.cbegin(), m_mem_checks.cend(),
|
|
|
|
[address](const auto& check) { return check.start_address == address; });
|
|
|
|
|
|
|
|
if (iter == m_mem_checks.cend())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Core::RunAsCPUThread([&] {
|
|
|
|
m_mem_checks.erase(iter);
|
2018-03-19 02:55:54 -04:00
|
|
|
if (!HasAny())
|
|
|
|
JitInterface::ClearCache();
|
2018-03-19 02:36:38 -04:00
|
|
|
PowerPC::DBATUpdated();
|
|
|
|
});
|
2011-02-25 09:35:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 21:57:09 +09:00
|
|
|
void MemChecks::Clear()
|
|
|
|
{
|
|
|
|
Core::RunAsCPUThread([&] {
|
|
|
|
m_mem_checks.clear();
|
|
|
|
JitInterface::ClearCache();
|
|
|
|
PowerPC::DBATUpdated();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-02 20:07:24 -05:00
|
|
|
TMemCheck* MemChecks::GetMemCheck(u32 address, size_t size)
|
2011-02-25 09:35:56 +00:00
|
|
|
{
|
2018-03-19 02:36:38 -04:00
|
|
|
const auto iter =
|
|
|
|
std::find_if(m_mem_checks.begin(), m_mem_checks.end(), [address, size](const auto& mc) {
|
|
|
|
return mc.end_address >= address && address + size - 1 >= mc.start_address;
|
|
|
|
});
|
|
|
|
|
|
|
|
// None found
|
|
|
|
if (iter == m_mem_checks.cend())
|
|
|
|
return nullptr;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-03-19 02:36:38 -04:00
|
|
|
return &*iter;
|
2008-12-20 14:00:33 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 09:42:16 -04:00
|
|
|
bool MemChecks::OverlapsMemcheck(u32 address, u32 length) const
|
2017-02-28 17:32:17 -05:00
|
|
|
{
|
|
|
|
if (!HasAny())
|
|
|
|
return false;
|
2018-03-19 02:36:38 -04:00
|
|
|
|
|
|
|
const u32 page_end_suffix = length - 1;
|
|
|
|
const u32 page_end_address = address | page_end_suffix;
|
|
|
|
|
|
|
|
return std::any_of(m_mem_checks.cbegin(), m_mem_checks.cend(), [&](const auto& mc) {
|
|
|
|
return ((mc.start_address | page_end_suffix) == page_end_address ||
|
|
|
|
(mc.end_address | page_end_suffix) == page_end_address) ||
|
|
|
|
((mc.start_address | page_end_suffix) < page_end_address &&
|
|
|
|
(mc.end_address | page_end_suffix) > page_end_address);
|
|
|
|
});
|
2017-02-28 17:32:17 -05:00
|
|
|
}
|
|
|
|
|
2021-08-31 11:35:10 -04:00
|
|
|
bool TMemCheck::Action(Common::DebugInterface* debug_interface, u64 value, u32 addr, bool write,
|
2017-03-02 20:07:24 -05:00
|
|
|
size_t size, u32 pc)
|
2008-12-20 14:00:33 +00:00
|
|
|
{
|
2021-02-28 08:04:50 +00:00
|
|
|
if (!is_enabled)
|
|
|
|
return false;
|
|
|
|
|
2017-01-11 08:27:45 -05:00
|
|
|
if ((write && is_break_on_write) || (!write && is_break_on_read))
|
2011-02-25 09:35:56 +00:00
|
|
|
{
|
2017-01-11 08:27:45 -05:00
|
|
|
if (log_on_hit)
|
2011-02-25 09:35:56 +00:00
|
|
|
{
|
2020-11-25 08:45:21 -05:00
|
|
|
NOTICE_LOG_FMT(MEMMAP, "MBP {:08x} ({}) {}{} {:x} at {:08x} ({})", pc,
|
|
|
|
debug_interface->GetDescription(pc), write ? "Write" : "Read", size * 8, value,
|
|
|
|
addr, debug_interface->GetDescription(addr));
|
2011-02-25 09:35:56 +00:00
|
|
|
}
|
2017-01-11 08:27:45 -05:00
|
|
|
if (break_on_hit)
|
2016-09-23 21:15:35 -04:00
|
|
|
return true;
|
2011-02-25 09:35:56 +00:00
|
|
|
}
|
2015-04-23 01:22:35 -04:00
|
|
|
return false;
|
2008-07-17 21:46:34 +00:00
|
|
|
}
|