Permalink
Browse files

Debugger: Add mutexing to disassembly manager.

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 15, 2018
1 parent e313a9b commit 0b8359fd77335bf68272a5c6f6380a58582df8d5
Showing with 41 additions and 12 deletions.
  1. +37 −12 Core/Debugger/DisassemblyManager.cpp
  2. +4 −0 Core/Debugger/DisassemblyManager.h
@@ -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;
@@ -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;
@@ -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())
{
@@ -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())
{
@@ -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())
{
@@ -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);
@@ -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);
@@ -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;
@@ -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;
@@ -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;
@@ -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];
@@ -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)
{
@@ -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)
{
@@ -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;
}
}
}
@@ -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();
@@ -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);
@@ -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++)
{
@@ -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;
@@ -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;
@@ -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())
{
@@ -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();
@@ -17,6 +17,7 @@
#pragma once
#include <mutex>
#include "Common/CommonTypes.h"
#include "Core/Debugger/SymbolMap.h"
#include "Core/MIPS/MIPSAnalyst.h"
@@ -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
@@ -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
@@ -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;
};

0 comments on commit 0b8359f

Please sign in to comment.