Skip to content

Commit

Permalink
Merge pull request #19722 from hrydgard/more-cleanup
Browse files Browse the repository at this point in the history
More GE debugger code cleanup
  • Loading branch information
hrydgard authored Dec 12, 2024
2 parents 3d0dced + 8d1fbe9 commit d32ac2c
Show file tree
Hide file tree
Showing 18 changed files with 623 additions and 594 deletions.
30 changes: 12 additions & 18 deletions Core/Debugger/DisassemblyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
#include "Core/Debugger/SymbolMap.h"
#include "Core/Debugger/DisassemblyManager.h"

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

bool isInInterval(u32 start, u32 size, u32 value) {
return start <= value && value <= (start+size-1);
Expand Down Expand Up @@ -185,6 +182,11 @@ std::map<u32,DisassemblyEntry*>::iterator findDisassemblyEntry(std::map<u32,Disa
return entries.end();
}

void DisassemblyManager::setCpu(DebugInterface *cpu) {
_dbg_assert_(cpu);
cpu_ = cpu;
};

void DisassemblyManager::analyze(u32 address, u32 size = 1024)
{
u32 end = address+size;
Expand Down Expand Up @@ -519,7 +521,7 @@ void DisassemblyFunction::generateBranchLines()
u32 end = address+size;

std::lock_guard<std::recursive_mutex> guard(lock_);
DebugInterface *cpu = DisassemblyManager::getCpu();
DebugInterface *cpu = g_disassemblyManager.getCpu();
for (u32 funcPos = address; funcPos < end; funcPos += 4)
{
MIPSAnalyst::MipsOpcodeInfo opInfo = MIPSAnalyst::GetOpcodeInfo(cpu, funcPos);
Expand Down Expand Up @@ -612,7 +614,7 @@ void DisassemblyFunction::load()
}
}

DebugInterface *cpu = DisassemblyManager::getCpu();
DebugInterface *cpu = g_disassemblyManager.getCpu();
u32 funcPos = address;
u32 funcEnd = address+size;

Expand Down Expand Up @@ -747,11 +749,7 @@ void DisassemblyFunction::clear()
hash = 0;
}

bool DisassemblyOpcode::disassemble(u32 address, DisassemblyLineInfo &dest, bool insertSymbols, DebugInterface *cpuDebug)
{
if (!cpuDebug)
cpuDebug = DisassemblyManager::getCpu();

bool DisassemblyOpcode::disassemble(u32 address, DisassemblyLineInfo &dest, bool insertSymbols, DebugInterface *cpuDebug) {
char opcode[64],arguments[256];
char dizz[512];
DisAsm(address, dizz, sizeof(dizz));
Expand All @@ -778,7 +776,7 @@ void DisassemblyOpcode::getBranchLines(u32 start, u32 size, std::vector<BranchLi
int lane = 0;
for (u32 pos = start; pos < start+size; pos += 4)
{
MIPSAnalyst::MipsOpcodeInfo info = MIPSAnalyst::GetOpcodeInfo(DisassemblyManager::getCpu(),pos);
MIPSAnalyst::MipsOpcodeInfo info = MIPSAnalyst::GetOpcodeInfo(g_disassemblyManager.getCpu(),pos);
if (info.isBranch && !info.isBranchToRegister && !info.isLinkedBranch) {
if (!Memory::IsValidAddress(info.branchTarget))
continue;
Expand Down Expand Up @@ -823,9 +821,6 @@ void DisassemblyMacro::setMacroMemory(std::string_view _name, u32 _immediate, u8

bool DisassemblyMacro::disassemble(u32 address, DisassemblyLineInfo &dest, bool insertSymbols, DebugInterface *cpuDebug)
{
if (!cpuDebug)
cpuDebug = DisassemblyManager::getCpu();

char buffer[64];
dest.type = DISTYPE_MACRO;
dest.info = MIPSAnalyst::GetOpcodeInfo(cpuDebug, address);
Expand Down Expand Up @@ -875,7 +870,6 @@ bool DisassemblyMacro::disassemble(u32 address, DisassemblyLineInfo &dest, bool
return true;
}


DisassemblyData::DisassemblyData(u32 _address, u32 _size, DataType _type): address(_address), size(_size), type(_type)
{
_dbg_assert_(PSP_IsInited());
Expand Down Expand Up @@ -950,8 +944,8 @@ void DisassemblyData::createLines()
lineAddresses.clear();

u32 pos = address;
u32 end = address+size;
u32 maxChars = DisassemblyManager::getMaxParamChars();
const u32 end = address+size;
const u32 maxChars = g_disassemblyManager.getMaxParamChars();

std::string currentLine;
u32 currentLineStart = pos;
Expand Down
19 changes: 11 additions & 8 deletions Core/Debugger/DisassemblyManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,27 @@ class DisassemblyManager {

void clear();

static void setCpu(DebugInterface *cpu) { cpu_ = cpu; };
void setCpu(DebugInterface *cpu);
void setMaxParamChars(int num) { maxParamChars = num; clear(); };
void getLine(u32 address, bool insertSymbols, DisassemblyLineInfo &dest, DebugInterface *cpuDebug = nullptr);
void getLine(u32 address, bool insertSymbols, DisassemblyLineInfo &dest, DebugInterface *cpuDebug);
void analyze(u32 address, u32 size);
std::vector<BranchLine> getBranchLines(u32 start, u32 size);

u32 getStartAddress(u32 address);
u32 getNthPreviousAddress(u32 address, int n = 1);
u32 getNthNextAddress(u32 address, int n = 1);

static DebugInterface *getCpu() { return cpu_; };
static int getMaxParamChars() { return maxParamChars; };
DebugInterface *getCpu() { return cpu_; };
int getMaxParamChars() { return maxParamChars; };

private:
static std::map<u32,DisassemblyEntry*> entries;
static std::recursive_mutex entriesLock_;
static DebugInterface *cpu_;
static int maxParamChars;
std::map<u32,DisassemblyEntry*> entries;
std::recursive_mutex entriesLock_;
DebugInterface *cpu_;
int maxParamChars = 29;
};

extern DisassemblyManager g_disassemblyManager;

bool isInInterval(u32 start, u32 size, u32 value);
bool IsLikelyStringAt(uint32_t addr);
3 changes: 1 addition & 2 deletions Core/Debugger/WebSocket/BreakpointSubscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,8 @@ void WebSocketCPUBreakpointList(DebuggerRequest &req) {
else
json.writeString("symbol", symbol);

DisassemblyManager manager;
DisassemblyLineInfo line;
manager.getLine(manager.getStartAddress(bp.addr), true, line);
g_disassemblyManager.getLine(g_disassemblyManager.getStartAddress(bp.addr), true, line, currentDebugMIPS);
json.writeString("code", line.name + " " + line.params);

json.pop();
Expand Down
26 changes: 12 additions & 14 deletions Core/Debugger/WebSocket/DisasmSubscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
class WebSocketDisasmState : public DebuggerSubscriber {
public:
WebSocketDisasmState() {
disasm_.setCpu(currentDebugMIPS);
g_disassemblyManager.setCpu(currentDebugMIPS);
}
~WebSocketDisasmState() {
disasm_.clear();
g_disassemblyManager.clear();
}

void Base(DebuggerRequest &req);
Expand All @@ -48,8 +48,6 @@ class WebSocketDisasmState : public DebuggerSubscriber {
protected:
void WriteDisasmLine(JsonWriter &json, const DisassemblyLineInfo &l);
void WriteBranchGuide(JsonWriter &json, const BranchLine &l);

DisassemblyManager disasm_;
};

DebuggerSubscriber *WebSocketDisasmInit(DebuggerEventHandlerMap &map) {
Expand Down Expand Up @@ -316,18 +314,18 @@ void WebSocketDisasmState::Disasm(DebuggerRequest &req) {
if (count != 0) {
count = std::min(count, MAX_RANGE);
// Let's assume everything is two instructions.
disasm_.analyze(start - 4, count * 8 + 8);
start = disasm_.getStartAddress(start);
g_disassemblyManager.analyze(start - 4, count * 8 + 8);
start = g_disassemblyManager.getStartAddress(start);
if (start == -1)
req.ParamU32("address", &start);
end = disasm_.getNthNextAddress(start, count);
end = g_disassemblyManager.getNthNextAddress(start, count);
} else if (req.ParamU32("end", &end)) {
end = std::max(start, end);
if (end - start > MAX_RANGE * 4)
end = start + MAX_RANGE * 4;
// Let's assume everything is two instructions at most.
disasm_.analyze(start - 4, end - start + 8);
start = disasm_.getStartAddress(start);
g_disassemblyManager.analyze(start - 4, end - start + 8);
start = g_disassemblyManager.getStartAddress(start);
if (start == -1)
req.ParamU32("address", &start);
// Correct end and calculate count based on it.
Expand All @@ -336,11 +334,11 @@ void WebSocketDisasmState::Disasm(DebuggerRequest &req) {
u32 next = start;
count = 0;
if (stop < start) {
for (next = start; next > stop; next = disasm_.getNthNextAddress(next, 1)) {
for (next = start; next > stop; next = g_disassemblyManager.getNthNextAddress(next, 1)) {
count++;
}
}
for (end = next; end < stop && end >= next; end = disasm_.getNthNextAddress(end, 1)) {
for (end = next; end < stop && end >= next; end = g_disassemblyManager.getNthNextAddress(end, 1)) {
count++;
}
} else {
Expand All @@ -362,7 +360,7 @@ void WebSocketDisasmState::Disasm(DebuggerRequest &req) {
DisassemblyLineInfo line;
uint32_t addr = start;
for (uint32_t i = 0; i < count; ++i) {
disasm_.getLine(addr, displaySymbols, line, cpuDebug);
g_disassemblyManager.getLine(addr, displaySymbols, line, cpuDebug);
WriteDisasmLine(json, line);
addr += line.totalSize;

Expand All @@ -373,7 +371,7 @@ void WebSocketDisasmState::Disasm(DebuggerRequest &req) {
json.pop();

json.pushArray("branchGuides");
auto branchGuides = disasm_.getBranchLines(start, end - start);
auto branchGuides = g_disassemblyManager.getBranchLines(start, end - start);
for (auto bl : branchGuides)
WriteBranchGuide(json, bl);
json.pop();
Expand Down Expand Up @@ -428,7 +426,7 @@ void WebSocketDisasmState::SearchDisasm(DebuggerRequest &req) {
bool found = false;
uint32_t addr = start;
do {
disasm_.getLine(addr, displaySymbols, line, cpuDebug);
g_disassemblyManager.getLine(addr, displaySymbols, line, cpuDebug);
const std::string addressSymbol = g_symbolMap->GetLabelString(addr);

std::string mergeForSearch;
Expand Down
12 changes: 4 additions & 8 deletions Core/Debugger/WebSocket/HLESubscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ void WebSocketHLEFuncAdd(DebuggerRequest &req) {
}

// Clear cache for branch lines and such.
DisassemblyManager manager;
manager.clear();
g_disassemblyManager.clear();

JsonWriter &json = req.Respond();
json.writeUint("address", addr);
Expand Down Expand Up @@ -354,8 +353,7 @@ void WebSocketHLEFuncRemove(DebuggerRequest &req) {
}

// Clear cache for branch lines and such.
DisassemblyManager manager;
manager.clear();
g_disassemblyManager.clear();

JsonWriter &json = req.Respond();
json.writeUint("address", funcBegin);
Expand Down Expand Up @@ -392,8 +390,7 @@ static u32 RemoveFuncSymbolsInRange(u32 addr, u32 size) {
}

// Clear cache for branch lines and such.
DisassemblyManager manager;
manager.clear();
g_disassemblyManager.clear();
}
return counter;
}
Expand Down Expand Up @@ -592,9 +589,8 @@ void WebSocketHLEBacktrace(DebuggerRequest &req) {
json.writeUint("sp", f.sp);
json.writeUint("stackSize", f.stackSize);

DisassemblyManager manager;
DisassemblyLineInfo line;
manager.getLine(manager.getStartAddress(f.pc), true, line, cpuDebug);
g_disassemblyManager.getLine(g_disassemblyManager.getStartAddress(f.pc), true, line, cpuDebug);
json.writeString("code", line.name + " " + line.params);

json.pop();
Expand Down
10 changes: 4 additions & 6 deletions Core/Debugger/WebSocket/SteppingSubscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ using namespace MIPSAnalyst;

struct WebSocketSteppingState : public DebuggerSubscriber {
WebSocketSteppingState() {
disasm_.setCpu(currentDebugMIPS);
g_disassemblyManager.setCpu(currentDebugMIPS);
}
~WebSocketSteppingState() {
disasm_.clear();
g_disassemblyManager.clear();
}

void Into(DebuggerRequest &req);
Expand All @@ -47,8 +47,6 @@ struct WebSocketSteppingState : public DebuggerSubscriber {
int GetNextInstructionCount(DebugInterface *cpuDebug);
void PrepareResume();
void AddThreadCondition(uint32_t breakpointAddress, uint32_t threadID);

DisassemblyManager disasm_;
};

DebuggerSubscriber *WebSocketSteppingInit(DebuggerEventHandlerMap &map) {
Expand Down Expand Up @@ -266,8 +264,8 @@ void WebSocketSteppingState::HLE(DebuggerRequest &req) {
}

uint32_t WebSocketSteppingState::GetNextAddress(DebugInterface *cpuDebug) {
uint32_t current = disasm_.getStartAddress(cpuDebug->GetPC());
return disasm_.getNthNextAddress(current, 1);
uint32_t current = g_disassemblyManager.getStartAddress(cpuDebug->GetPC());
return g_disassemblyManager.getNthNextAddress(current, 1);
}

int WebSocketSteppingState::GetNextInstructionCount(DebugInterface *cpuDebug) {
Expand Down
Loading

0 comments on commit d32ac2c

Please sign in to comment.