#pragma once
#include <vector>
#include <atomic>
#include <mutex>
#include "Core/MIPS/MIPSDebugInterface.h"
#include "Common/Math/expression_parser.h"
enum BreakAction : u32 {
BREAK_ACTION_IGNORE = 0x00,
BREAK_ACTION_LOG = 0x01,
BREAK_ACTION_PAUSE = 0x02,
};
static inline BreakAction &operator |= (BreakAction &lhs, const BreakAction &rhs) {
lhs = BreakAction(lhs | rhs);
return lhs;
}
static inline BreakAction operator | (const BreakAction &lhs, const BreakAction &rhs) {
return BreakAction((u32)lhs | (u32)rhs);
}
struct BreakPointCond {
DebugInterface *debug = nullptr;
PostfixExpression expression;
std::string expressionString;
u32 Evaluate() {
u32 result;
if (parseExpression(debug, expression, result) == false)
return 0;
return result;
}
};
struct BreakPoint {
u32 addr;
bool temporary;
BreakAction result = BREAK_ACTION_IGNORE;
std::string logFormat;
bool hasCond = false;
BreakPointCond cond;
bool IsEnabled() const {
return (result & BREAK_ACTION_PAUSE) != 0;
}
bool operator == (const BreakPoint &other) const {
return addr == other.addr;
}
bool operator < (const BreakPoint &other) const {
return addr < other.addr;
}
};
enum MemCheckCondition {
MEMCHECK_READ = 0x01,
MEMCHECK_WRITE = 0x02,
MEMCHECK_WRITE_ONCHANGE = 0x04,
MEMCHECK_READWRITE = 0x03,
};
struct MemCheck {
u32 start;
u32 end;
MemCheckCondition cond = MEMCHECK_READ;
BreakAction result = BREAK_ACTION_IGNORE;
std::string logFormat;
bool hasCondition = false;
BreakPointCond condition;
u32 numHits = 0;
u32 lastPC = 0;
u32 lastAddr = 0;
int lastSize = 0;
BreakAction Apply(u32 addr, bool write, int size, u32 pc);
BreakAction Action(u32 addr, bool write, int size, u32 pc, const char *reason);
void Log(u32 addr, bool write, int size, u32 pc, const char *reason) const;
bool IsEnabled() const {
return (result & BREAK_ACTION_PAUSE) != 0;
}
bool operator == (const MemCheck &other) const {
return start == other.start && end == other.end;
}
};
class BreakpointManager {
public:
static const size_t INVALID_BREAKPOINT = -1;
static const size_t INVALID_MEMCHECK = -1;
bool IsAddressBreakPoint(u32 addr);
bool IsAddressBreakPoint(u32 addr, bool* enabled);
bool IsTempBreakPoint(u32 addr);
bool RangeContainsBreakPoint(u32 addr, u32 size);
int AddBreakPoint(u32 addr, bool temp = false);
void RemoveBreakPoint(u32 addr);
void ChangeBreakPoint(u32 addr, bool enable);
void ChangeBreakPoint(u32 addr, BreakAction result);
void ClearAllBreakPoints();
void ClearTemporaryBreakPoints();
void ChangeBreakPointAddCond(u32 addr, const BreakPointCond &cond);
void ChangeBreakPointRemoveCond(u32 addr);
BreakPointCond *GetBreakPointCondition(u32 addr);
void ChangeBreakPointLogFormat(u32 addr, const std::string &fmt);
BreakAction ExecBreakPoint(u32 addr);
int AddMemCheck(u32 start, u32 end, MemCheckCondition cond, BreakAction result);
void RemoveMemCheck(u32 start, u32 end);
void ChangeMemCheck(u32 start, u32 end, MemCheckCondition cond, BreakAction result);
void ClearAllMemChecks();
void ChangeMemCheckAddCond(u32 start, u32 end, const BreakPointCond &cond);
void ChangeMemCheckRemoveCond(u32 start, u32 end);
BreakPointCond *GetMemCheckCondition(u32 start, u32 end);
void ChangeMemCheckLogFormat(u32 start, u32 end, const std::string &fmt);
bool GetMemCheck(u32 start, u32 end, MemCheck *check);
bool GetMemCheckInRange(u32 address, int size, MemCheck *check);
BreakAction ExecMemCheck(u32 address, bool write, int size, u32 pc, const char *reason);
BreakAction ExecOpMemCheck(u32 address, u32 pc);
void SetSkipFirst(u32 pc);
u32 CheckSkipFirst();
std::vector<MemCheck> GetMemCheckRanges(bool write);
std::vector<MemCheck> GetMemChecks();
std::vector<BreakPoint> GetBreakpoints();
std::vector<BreakPoint> &GetBreakpointRefs() {
return breakPoints_;
}
std::vector<MemCheck> &GetMemCheckRefs() {
return memChecks_;
}
bool HasBreakPoints() const {
return anyBreakPoints_;
}
bool HasMemChecks() const {
return anyMemChecks_;
}
void Frame();
bool ValidateLogFormat(MIPSDebugInterface *cpu, const std::string &fmt);
bool EvaluateLogFormat(MIPSDebugInterface *cpu, const std::string &fmt, std::string &result);
private:
void Update(u32 addr = 0) {
needsUpdate_ = true;
updateAddr_ = addr;
}
size_t FindBreakpoint(u32 addr, bool matchTemp = false, bool temp = false);
size_t FindMemCheck(u32 start, u32 end);
MemCheck *GetMemCheckLocked(u32 address, int size);
void UpdateCachedMemCheckRanges();
std::atomic<bool> anyBreakPoints_;
std::atomic<bool> anyMemChecks_;
std::mutex breakPointsMutex_;
std::mutex memCheckMutex_;
std::vector<BreakPoint> breakPoints_;
u32 breakSkipFirstAt_ = 0;
u64 breakSkipFirstTicks_ = 0;
std::vector<MemCheck> memChecks_;
std::vector<MemCheck> memCheckRangesRead_;
std::vector<MemCheck> memCheckRangesWrite_;
bool needsUpdate_ = true;
u32 updateAddr_ = 0;
};
extern BreakpointManager g_breakpoints;