Skip to content

Commit

Permalink
Debugger: Use safe VTLB functions for memory reading and writing
Browse files Browse the repository at this point in the history
  • Loading branch information
F0bes committed Sep 29, 2024
1 parent 84d1680 commit a694c1b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
66 changes: 51 additions & 15 deletions pcsx2/DebugTools/DebugInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class MipsExpressionFunctions : public IExpressionFunctions
return m_cpu->getLO()._u64[0];
if (referenceIndex & REF_INDEX_IS_OPSL)
{
const u32 OP = memRead32(m_cpu->getPC());
const u32 OP = m_cpu->read32(m_cpu->getPC());
const R5900::OPCODE& opcode = R5900::GetInstruction(OP);
if (opcode.flags & IS_MEMORY)
{
Expand Down Expand Up @@ -393,15 +393,23 @@ u32 R5900DebugInterface::read8(u32 address)
if (!isValidAddress(address))
return -1;

return memRead8(address);
u8 value;
if (!vtlb_memSafeReadBytes(address, &value, sizeof(u8)))
return -1;

return value;
}

u32 R5900DebugInterface::read8(u32 address, bool& valid)
{
if (!(valid = isValidAddress(address)))
return -1;

return memRead8(address);
u8 value;
if (!(valid = vtlb_memSafeReadBytes(address, &value, sizeof(u8))))
return -1;

return value;
}


Expand All @@ -410,47 +418,71 @@ u32 R5900DebugInterface::read16(u32 address)
if (!isValidAddress(address) || address % 2)
return -1;

return memRead16(address);
u16 value;
if (!vtlb_memSafeReadBytes(address, &value, sizeof(u16)))
return -1;

return static_cast<u32>(value);
}

u32 R5900DebugInterface::read16(u32 address, bool& valid)
{
if (!(valid = (isValidAddress(address) || address % 2)))
return -1;

return memRead16(address);
u16 value;
if (!(valid = vtlb_memSafeReadBytes(address, &value, sizeof(u16))))
return -1;

return static_cast<u32>(value);
}

u32 R5900DebugInterface::read32(u32 address)
{
if (!isValidAddress(address) || address % 4)
return -1;

return memRead32(address);
u32 value;
if (!vtlb_memSafeReadBytes(address, &value, sizeof(u32)))
return -1;

return value;
}

u32 R5900DebugInterface::read32(u32 address, bool& valid)
{
if (!(valid = (isValidAddress(address) || address % 4)))
return -1;

return memRead32(address);
u32 value;
if (!(valid = vtlb_memSafeReadBytes(address, &value, sizeof(u32))))
return -1;

return value;
}

u64 R5900DebugInterface::read64(u32 address)
{
if (!isValidAddress(address) || address % 8)
return -1;

return memRead64(address);
u64 value;
if (!vtlb_memSafeReadBytes(address, &value, sizeof(u64)))
return -1;

return value;
}

u64 R5900DebugInterface::read64(u32 address, bool& valid)
{
if (!(valid = (isValidAddress(address) || address % 8)))
return -1;

return memRead64(address);
u64 value;
if (!(valid = vtlb_memSafeReadBytes(address, &value, sizeof(u64))))
return -1;

return value;
}

u128 R5900DebugInterface::read128(u32 address)
Expand All @@ -462,7 +494,11 @@ u128 R5900DebugInterface::read128(u32 address)
return result;
}

memRead128(address, result);
if (!vtlb_memSafeReadBytes(address, &result, sizeof(u128)))
{
result.hi = result.lo = -1;
}

return result;
}

Expand All @@ -471,39 +507,39 @@ void R5900DebugInterface::write8(u32 address, u8 value)
if (!isValidAddress(address))
return;

memWrite8(address, value);
vtlb_memSafeWriteBytes(address, &value, sizeof(u8));
}

void R5900DebugInterface::write16(u32 address, u16 value)
{
if (!isValidAddress(address))
return;

memWrite16(address, value);
vtlb_memSafeWriteBytes(address, &value, sizeof(u16));
}

void R5900DebugInterface::write32(u32 address, u32 value)
{
if (!isValidAddress(address))
return;

memWrite32(address, value);
vtlb_memSafeWriteBytes(address, &value, sizeof(u32));
}

void R5900DebugInterface::write64(u32 address, u64 value)
{
if (!isValidAddress(address))
return;

memWrite64(address, value);
vtlb_memSafeWriteBytes(address, &value, sizeof(u64));
}

void R5900DebugInterface::write128(u32 address, u128 value)
{
if (!isValidAddress(address))
return;

memWrite128(address, value);
vtlb_memSafeWriteBytes(address, &value, sizeof(u128));
}

int R5900DebugInterface::getRegisterCategoryCount()
Expand Down
10 changes: 5 additions & 5 deletions pcsx2/DebugTools/DisassemblyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static u32 computeHash(u32 address, u32 size)
u32 hash = 0xBACD7814;
while (address < end)
{
hash += memRead32(address);
hash += r5900Debug.read32(address);
address += 4;
}
return hash;
Expand Down Expand Up @@ -918,7 +918,7 @@ void DisassemblyData::createLines()
bool inString = false;
while (pos < end)
{
u8 b = memRead8(pos++);
u8 b = r5900Debug.read8(pos++);
if (b >= 0x20 && b <= 0x7F)
{
if (currentLine.size()+1 >= maxChars)
Expand Down Expand Up @@ -995,18 +995,18 @@ void DisassemblyData::createLines()
switch (type)
{
case DATATYPE_BYTE:
value = memRead8(pos);
value = cpu->read8(pos);
std::snprintf(buffer,std::size(buffer),"0x%02X",value);
pos++;
break;
case DATATYPE_HALFWORD:
value = memRead16(pos);
value = cpu->read16(pos);
std::snprintf(buffer,std::size(buffer),"0x%04X",value);
pos += 2;
break;
case DATATYPE_WORD:
{
value = memRead32(pos);
value = cpu->read32(pos);
const std::string label = cpu->GetSymbolGuardian().SymbolStartingAtAddress(value).name;
if (!label.empty())
std::snprintf(buffer,std::size(buffer),"%s",label.c_str());
Expand Down

0 comments on commit a694c1b

Please sign in to comment.