Skip to content

Commit

Permalink
Debugger: Add mutexing to disassembly manager.
Browse files Browse the repository at this point in the history
It was crashing when accessing from another thread.

If in the future we go down to only the remote debugger, we could
potentially remove this.
  • Loading branch information
unknownbrackets committed Apr 29, 2018
1 parent ce17864 commit e290f6c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
49 changes: 37 additions & 12 deletions Core/Debugger/DisassemblyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "Core/Debugger/DisassemblyManager.h"

std::map<u32, DisassemblyEntry*> DisassemblyManager::entries;
std::recursive_mutex DisassemblyManager::entriesLock_;
DebugInterface* DisassemblyManager::cpu;
int DisassemblyManager::maxParamChars = 29;

Expand Down Expand Up @@ -149,7 +150,8 @@ void DisassemblyManager::analyze(u32 address, u32 size = 1024)
if (!PSP_IsInited())
return;

auto it = findDisassemblyEntry(entries,address,false);
std::lock_guard<std::recursive_mutex> guard(entriesLock_);
auto it = findDisassemblyEntry(entries, address, false);
if (it != entries.end())
{
DisassemblyEntry* entry = it->second;
Expand Down Expand Up @@ -219,7 +221,8 @@ void DisassemblyManager::analyze(u32 address, u32 size = 1024)
std::vector<BranchLine> DisassemblyManager::getBranchLines(u32 start, u32 size)
{
std::vector<BranchLine> result;


std::lock_guard<std::recursive_mutex> guard(entriesLock_);
auto it = findDisassemblyEntry(entries,start,false);
if (it != entries.end())
{
Expand All @@ -235,6 +238,7 @@ std::vector<BranchLine> DisassemblyManager::getBranchLines(u32 start, u32 size)

void DisassemblyManager::getLine(u32 address, bool insertSymbols, DisassemblyLineInfo& dest)
{
std::lock_guard<std::recursive_mutex> guard(entriesLock_);
auto it = findDisassemblyEntry(entries,address,false);
if (it == entries.end())
{
Expand Down Expand Up @@ -267,6 +271,7 @@ void DisassemblyManager::getLine(u32 address, bool insertSymbols, DisassemblyLin

u32 DisassemblyManager::getStartAddress(u32 address)
{
std::lock_guard<std::recursive_mutex> guard(entriesLock_);
auto it = findDisassemblyEntry(entries,address,false);
if (it == entries.end())
{
Expand All @@ -283,6 +288,7 @@ u32 DisassemblyManager::getStartAddress(u32 address)

u32 DisassemblyManager::getNthPreviousAddress(u32 address, int n)
{
std::lock_guard<std::recursive_mutex> guard(entriesLock_);
while (Memory::IsValidAddress(address))
{
auto it = findDisassemblyEntry(entries,address,false);
Expand Down Expand Up @@ -311,6 +317,7 @@ u32 DisassemblyManager::getNthPreviousAddress(u32 address, int n)

u32 DisassemblyManager::getNthNextAddress(u32 address, int n)
{
std::lock_guard<std::recursive_mutex> guard(entriesLock_);
while (Memory::IsValidAddress(address))
{
auto it = findDisassemblyEntry(entries,address,false);
Expand Down Expand Up @@ -345,6 +352,7 @@ DisassemblyManager::~DisassemblyManager() {

void DisassemblyManager::clear()
{
std::lock_guard<std::recursive_mutex> guard(entriesLock_);
for (auto it = entries.begin(); it != entries.end(); it++)
{
delete it->second;
Expand Down Expand Up @@ -383,11 +391,13 @@ void DisassemblyFunction::recheck()

int DisassemblyFunction::getNumLines()
{
std::lock_guard<std::recursive_mutex> guard(lock_);
return (int) lineAddresses.size();
}

int DisassemblyFunction::getLineNum(u32 address, bool findStart)
{
std::lock_guard<std::recursive_mutex> guard(lock_);
if (findStart)
{
int last = (int)lineAddresses.size() - 1;
Expand Down Expand Up @@ -418,11 +428,13 @@ int DisassemblyFunction::getLineNum(u32 address, bool findStart)

u32 DisassemblyFunction::getLineAddress(int line)
{
std::lock_guard<std::recursive_mutex> guard(lock_);
return lineAddresses[line];
}

bool DisassemblyFunction::disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols)
{
std::lock_guard<std::recursive_mutex> guard(lock_);
auto it = findDisassemblyEntry(entries,address,false);
if (it == entries.end())
return false;
Expand All @@ -434,6 +446,7 @@ void DisassemblyFunction::getBranchLines(u32 start, u32 size, std::vector<Branch
{
u32 end = start+size;

std::lock_guard<std::recursive_mutex> guard(lock_);
for (size_t i = 0; i < lines.size(); i++)
{
BranchLine& line = lines[i];
Expand Down Expand Up @@ -466,6 +479,7 @@ void DisassemblyFunction::generateBranchLines()

u32 end = address+size;

std::lock_guard<std::recursive_mutex> guard(lock_);
DebugInterface* cpu = DisassemblyManager::getCpu();
for (u32 funcPos = address; funcPos < end; funcPos += 4)
{
Expand Down Expand Up @@ -524,6 +538,7 @@ void DisassemblyFunction::generateBranchLines()
void DisassemblyFunction::addOpcodeSequence(u32 start, u32 end)
{
DisassemblyOpcode* opcode = new DisassemblyOpcode(start,(end-start)/4);
std::lock_guard<std::recursive_mutex> guard(lock_);
entries[start] = opcode;
for (u32 pos = start; pos < end; pos += 4)
{
Expand All @@ -537,18 +552,21 @@ void DisassemblyFunction::load()

// gather all branch targets
std::set<u32> branchTargets;
for (size_t i = 0; i < lines.size(); i++)
{
switch (lines[i].type)
std::lock_guard<std::recursive_mutex> guard(lock_);
for (size_t i = 0; i < lines.size(); i++)
{
case LINE_DOWN:
branchTargets.insert(lines[i].second);
break;
case LINE_UP:
branchTargets.insert(lines[i].first);
break;
default:
break;
switch (lines[i].type)
{
case LINE_DOWN:
branchTargets.insert(lines[i].second);
break;
case LINE_UP:
branchTargets.insert(lines[i].first);
break;
default:
break;
}
}
}

Expand All @@ -566,6 +584,7 @@ void DisassemblyFunction::load()
addOpcodeSequence(opcodeSequenceStart,funcPos);

DisassemblyData* data = new DisassemblyData(funcPos,g_symbolMap->GetDataSize(funcPos),g_symbolMap->GetDataType(funcPos));
std::lock_guard<std::recursive_mutex> guard(lock_);
entries[funcPos] = data;
lineAddresses.push_back(funcPos);
funcPos += data->getTotalSize();
Expand All @@ -581,6 +600,7 @@ void DisassemblyFunction::load()
u32 nextPos = (funcPos+3) & ~3;

DisassemblyComment* comment = new DisassemblyComment(funcPos,nextPos-funcPos,".align","4");
std::lock_guard<std::recursive_mutex> guard(lock_);
entries[funcPos] = comment;
lineAddresses.push_back(funcPos);

Expand Down Expand Up @@ -665,6 +685,7 @@ void DisassemblyFunction::load()
if (opcodeSequenceStart != opAddress)
addOpcodeSequence(opcodeSequenceStart,opAddress);

std::lock_guard<std::recursive_mutex> guard(lock_);
entries[opAddress] = macro;
for (int i = 0; i < macro->getNumLines(); i++)
{
Expand All @@ -686,6 +707,7 @@ void DisassemblyFunction::load()

void DisassemblyFunction::clear()
{
std::lock_guard<std::recursive_mutex> guard(lock_);
for (auto it = entries.begin(); it != entries.end(); it++)
{
delete it->second;
Expand Down Expand Up @@ -866,6 +888,7 @@ bool DisassemblyData::disassemble(u32 address, DisassemblyLineInfo& dest, bool i
return false;
}

std::lock_guard<std::recursive_mutex> guard(lock_);
auto it = lines.find(address);
if (it == lines.end())
return false;
Expand All @@ -877,6 +900,7 @@ bool DisassemblyData::disassemble(u32 address, DisassemblyLineInfo& dest, bool i

int DisassemblyData::getLineNum(u32 address, bool findStart)
{
std::lock_guard<std::recursive_mutex> guard(lock_);
auto it = lines.upper_bound(address);
if (it != lines.end())
{
Expand All @@ -891,6 +915,7 @@ int DisassemblyData::getLineNum(u32 address, bool findStart)

void DisassemblyData::createLines()
{
std::lock_guard<std::recursive_mutex> guard(lock_);
lines.clear();
lineAddresses.clear();

Expand Down
4 changes: 4 additions & 0 deletions Core/Debugger/DisassemblyManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#pragma once

#include <mutex>
#include "Common/CommonTypes.h"
#include "Core/Debugger/SymbolMap.h"
#include "Core/MIPS/MIPSAnalyst.h"
Expand Down Expand Up @@ -91,6 +92,7 @@ class DisassemblyFunction: public DisassemblyEntry
std::vector<BranchLine> lines;
std::map<u32,DisassemblyEntry*> entries;
std::vector<u32> lineAddresses;
std::recursive_mutex lock_;
};

class DisassemblyOpcode: public DisassemblyEntry
Expand Down Expand Up @@ -169,6 +171,7 @@ class DisassemblyData: public DisassemblyEntry
DataType type;
std::map<u32,DataEntry> lines;
std::vector<u32> lineAddresses;
std::recursive_mutex lock_;
};

class DisassemblyComment: public DisassemblyEntry
Expand Down Expand Up @@ -214,6 +217,7 @@ class DisassemblyManager
static int getMaxParamChars() { return maxParamChars; };
private:
static std::map<u32,DisassemblyEntry*> entries;
static std::recursive_mutex entriesLock_;
static DebugInterface* cpu;
static int maxParamChars;
};
Expand Down

0 comments on commit e290f6c

Please sign in to comment.