From e290f6c05b59ab53435108a59347e40646c68bcf Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 15 Apr 2018 16:28:29 -0700 Subject: [PATCH] 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. --- Core/Debugger/DisassemblyManager.cpp | 49 +++++++++++++++++++++------- Core/Debugger/DisassemblyManager.h | 4 +++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Core/Debugger/DisassemblyManager.cpp b/Core/Debugger/DisassemblyManager.cpp index 4ff4c58fd3d2..4631c9e0362c 100644 --- a/Core/Debugger/DisassemblyManager.cpp +++ b/Core/Debugger/DisassemblyManager.cpp @@ -31,6 +31,7 @@ #include "Core/Debugger/DisassemblyManager.h" std::map 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 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 DisassemblyManager::getBranchLines(u32 start, u32 size) { std::vector result; - + + std::lock_guard guard(entriesLock_); auto it = findDisassemblyEntry(entries,start,false); if (it != entries.end()) { @@ -235,6 +238,7 @@ std::vector DisassemblyManager::getBranchLines(u32 start, u32 size) void DisassemblyManager::getLine(u32 address, bool insertSymbols, DisassemblyLineInfo& dest) { + std::lock_guard 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 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 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 guard(entriesLock_); while (Memory::IsValidAddress(address)) { auto it = findDisassemblyEntry(entries,address,false); @@ -345,6 +352,7 @@ DisassemblyManager::~DisassemblyManager() { void DisassemblyManager::clear() { + std::lock_guard 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 guard(lock_); return (int) lineAddresses.size(); } int DisassemblyFunction::getLineNum(u32 address, bool findStart) { + std::lock_guard 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 guard(lock_); return lineAddresses[line]; } bool DisassemblyFunction::disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols) { + std::lock_guard 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 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 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 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 branchTargets; - for (size_t i = 0; i < lines.size(); i++) { - switch (lines[i].type) + std::lock_guard 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 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 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 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 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 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 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 guard(lock_); lines.clear(); lineAddresses.clear(); diff --git a/Core/Debugger/DisassemblyManager.h b/Core/Debugger/DisassemblyManager.h index 9002f1f8c10e..7d84805b01e4 100644 --- a/Core/Debugger/DisassemblyManager.h +++ b/Core/Debugger/DisassemblyManager.h @@ -17,6 +17,7 @@ #pragma once +#include #include "Common/CommonTypes.h" #include "Core/Debugger/SymbolMap.h" #include "Core/MIPS/MIPSAnalyst.h" @@ -91,6 +92,7 @@ class DisassemblyFunction: public DisassemblyEntry std::vector lines; std::map entries; std::vector lineAddresses; + std::recursive_mutex lock_; }; class DisassemblyOpcode: public DisassemblyEntry @@ -169,6 +171,7 @@ class DisassemblyData: public DisassemblyEntry DataType type; std::map lines; std::vector lineAddresses; + std::recursive_mutex lock_; }; class DisassemblyComment: public DisassemblyEntry @@ -214,6 +217,7 @@ class DisassemblyManager static int getMaxParamChars() { return maxParamChars; }; private: static std::map entries; + static std::recursive_mutex entriesLock_; static DebugInterface* cpu; static int maxParamChars; };