Mesen-SX/Core/Debugger.cpp

365 lines
11 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Debugger.h"
2019-02-27 20:33:56 -05:00
#include "DebugTypes.h"
2019-02-15 21:33:13 -05:00
#include "Console.h"
#include "Cpu.h"
#include "Ppu.h"
#include "BaseCartridge.h"
#include "MemoryManager.h"
2019-03-07 20:12:32 -05:00
#include "SoundMixer.h"
#include "NotificationManager.h"
#include "CpuTypes.h"
#include "DisassemblyInfo.h"
#include "TraceLogger.h"
2019-02-15 21:33:13 -05:00
#include "MemoryDumper.h"
#include "CodeDataLogger.h"
#include "Disassembler.h"
2019-03-01 20:27:49 -05:00
#include "BreakpointManager.h"
2019-03-03 16:34:23 -05:00
#include "PpuTools.h"
2019-03-07 20:12:32 -05:00
#include "EventManager.h"
#include "EventType.h"
2019-03-24 12:05:51 -04:00
#include "CallstackManager.h"
2019-02-27 20:33:56 -05:00
#include "ExpressionEvaluator.h"
#include "../Utilities/HexUtilities.h"
#include "../Utilities/FolderUtilities.h"
2019-02-15 21:33:13 -05:00
Debugger::Debugger(shared_ptr<Console> console)
{
_console = console;
2019-02-15 21:33:13 -05:00
_cpu = console->GetCpu();
_ppu = console->GetPpu();
_memoryManager = console->GetMemoryManager();
2019-02-27 20:33:56 -05:00
_watchExpEval.reset(new ExpressionEvaluator(this));
_codeDataLogger.reset(new CodeDataLogger(console->GetCartridge()->DebugGetPrgRomSize()));
_disassembler.reset(new Disassembler(console, _codeDataLogger));
2019-03-07 20:12:32 -05:00
_traceLogger.reset(new TraceLogger(this, _console));
2019-03-22 21:38:31 -04:00
_memoryDumper.reset(new MemoryDumper(_ppu, console->GetSpc(), _memoryManager, console->GetCartridge()));
2019-03-01 20:27:49 -05:00
_breakpointManager.reset(new BreakpointManager(this));
2019-03-03 16:34:23 -05:00
_ppuTools.reset(new PpuTools(_console.get(), _ppu.get()));
2019-03-07 20:12:32 -05:00
_eventManager.reset(new EventManager(this, _cpu.get(), _ppu.get()));
2019-03-24 12:05:51 -04:00
_callstackManager.reset(new CallstackManager(this));
2019-03-07 20:12:32 -05:00
2019-03-23 15:39:21 -04:00
_cpuStepCount = -1;
2019-03-07 20:12:32 -05:00
_executionStopped = false;
_breakRequestCount = 0;
2019-03-12 12:28:41 -04:00
string cdlFile = FolderUtilities::CombinePath(FolderUtilities::GetDebuggerFolder(), FolderUtilities::GetFilename(_console->GetCartridge()->GetRomInfo().RomFile.GetFileName(), false) + ".cdl");
_codeDataLogger->LoadCdlFile(cdlFile);
//TODO: Thread safety
uint32_t prgRomSize = console->GetCartridge()->DebugGetPrgRomSize();
AddressInfo addrInfo;
addrInfo.Type = SnesMemoryType::PrgRom;
for(uint32_t i = 0; i < prgRomSize; i++) {
if(_codeDataLogger->IsCode(i)) {
addrInfo.Address = (int32_t)i;
i += _disassembler->BuildCache(addrInfo, _codeDataLogger->GetCpuFlags(i)) - 1;
}
}
}
Debugger::~Debugger()
{
Release();
}
void Debugger::Release()
{
2019-03-12 12:28:41 -04:00
string cdlFile = FolderUtilities::CombinePath(FolderUtilities::GetDebuggerFolder(), FolderUtilities::GetFilename(_console->GetCartridge()->GetRomInfo().RomFile.GetFileName(), false) + ".cdl");
_codeDataLogger->SaveCdlFile(cdlFile);
while(_executionStopped) {
Run();
}
}
void Debugger::ProcessCpuRead(uint32_t addr, uint8_t value, MemoryOperationType type)
{
AddressInfo addressInfo = _memoryManager->GetAbsoluteAddress(addr);
2019-03-01 20:27:49 -05:00
MemoryOperationInfo operation = { addr, value, type };
CpuState state = _cpu->GetState();
if(type == MemoryOperationType::ExecOpCode) {
if(addressInfo.Address >= 0) {
if(addressInfo.Type == SnesMemoryType::PrgRom) {
uint8_t flags = CdlFlags::Code | (state.PS & (CdlFlags::IndexMode8 | CdlFlags::MemoryMode8));
if(_prevOpCode == 0x20 || _prevOpCode == 0x5C || _prevOpCode == 0xDC || _prevOpCode == 0xFC) {
flags |= CdlFlags::SubEntryPoint;
}
_codeDataLogger->SetFlags(addressInfo.Address, flags);
}
_disassembler->BuildCache(addressInfo, state.PS);
}
DebugState debugState;
2019-03-01 20:27:49 -05:00
GetState(debugState);
DisassemblyInfo disInfo = _disassembler->GetDisassemblyInfo(addressInfo);
_traceLogger->Log(debugState, disInfo);
2019-03-24 12:05:51 -04:00
uint32_t pc = (state.K << 16) | state.PC;
if(_prevOpCode == 0x20 || _prevOpCode == 0x22 || _prevOpCode == 0xFC) {
//JSR, JSL
uint8_t opSize = DisassemblyInfo::GetOperandSize(_prevOpCode, 0) + 1;
uint32_t returnPc = (_prevProgramCounter & 0xFF0000) | (((_prevProgramCounter & 0xFFFF) + opSize) & 0xFFFF);
_callstackManager->Push(_prevProgramCounter, pc, returnPc, StackFrameFlags::None);
} else if(_prevOpCode == 0x60 || _prevOpCode == 0x6B || _prevOpCode == 0x40) {
//RTS, RTL, RTI
_callstackManager->Pop(pc);
}
ProcessStepConditions(_prevOpCode, pc);
_prevOpCode = value;
2019-03-24 12:05:51 -04:00
_prevProgramCounter = pc;
if(_cpuStepCount > 0) {
_cpuStepCount--;
2019-03-01 20:27:49 -05:00
}
2019-03-03 16:34:23 -05:00
/*if(value == 0x00 || value == 0xDB || value == 0x42) {
//Break on BRK/STP/WDM
2019-03-01 20:27:49 -05:00
_cpuStepCount = 0;
2019-03-03 16:34:23 -05:00
}*/
} else if(type == MemoryOperationType::ExecOperand) {
if(addressInfo.Type == SnesMemoryType::PrgRom && addressInfo.Address >= 0) {
_codeDataLogger->SetFlags(addressInfo.Address, CdlFlags::Code | (state.PS & (CdlFlags::IndexMode8 | CdlFlags::MemoryMode8)));
}
} else {
if(addressInfo.Type == SnesMemoryType::PrgRom && addressInfo.Address >= 0) {
_codeDataLogger->SetFlags(addressInfo.Address, CdlFlags::Data | (state.PS & (CdlFlags::IndexMode8 | CdlFlags::MemoryMode8)));
}
}
2019-03-01 20:27:49 -05:00
2019-03-07 20:12:32 -05:00
if(_memoryManager->IsRegister(addr)) {
_eventManager->AddEvent(DebugEventType::Register, operation);
}
2019-03-01 20:27:49 -05:00
ProcessBreakConditions(operation, addressInfo);
}
void Debugger::ProcessCpuWrite(uint32_t addr, uint8_t value, MemoryOperationType type)
{
AddressInfo addressInfo = _memoryManager->GetAbsoluteAddress(addr);
2019-03-01 20:27:49 -05:00
MemoryOperationInfo operation = { addr, value, type };
if(addressInfo.Address >= 0 && (addressInfo.Type == SnesMemoryType::WorkRam || addressInfo.Type == SnesMemoryType::SaveRam)) {
_disassembler->InvalidateCache(addressInfo);
}
2019-03-01 20:27:49 -05:00
2019-03-07 20:12:32 -05:00
if(_memoryManager->IsRegister(addr)) {
_eventManager->AddEvent(DebugEventType::Register, operation);
}
2019-03-01 20:27:49 -05:00
ProcessBreakConditions(operation, addressInfo);
}
void Debugger::ProcessWorkRamRead(uint32_t addr, uint8_t value)
{
AddressInfo addressInfo(addr, SnesMemoryType::WorkRam);
//TODO Make this more flexible/accurate
MemoryOperationInfo operation(0x7E0000 | addr, value, MemoryOperationType::Read);
ProcessBreakConditions(operation, addressInfo);
}
void Debugger::ProcessWorkRamWrite(uint32_t addr, uint8_t value)
{
AddressInfo addressInfo(addr, SnesMemoryType::WorkRam);
//TODO Make this more flexible/accurate
MemoryOperationInfo operation(0x7E0000 | addr, value, MemoryOperationType::Write);
ProcessBreakConditions(operation, addressInfo);
}
void Debugger::ProcessPpuRead(uint16_t addr, uint8_t value, SnesMemoryType memoryType)
{
AddressInfo addressInfo(addr, memoryType);
MemoryOperationInfo operation(addr, value, MemoryOperationType::Read);
ProcessBreakConditions(operation, addressInfo);
}
void Debugger::ProcessPpuWrite(uint16_t addr, uint8_t value, SnesMemoryType memoryType)
{
AddressInfo addressInfo(addr, memoryType);
MemoryOperationInfo operation(addr, value, MemoryOperationType::Write);
ProcessBreakConditions(operation, addressInfo);
}
2019-03-03 16:34:23 -05:00
void Debugger::ProcessPpuCycle()
{
uint16_t scanline = _ppu->GetState().Scanline;
uint16_t cycle = _ppu->GetState().Cycle;
_ppuTools->UpdateViewers(scanline, cycle);
if(_ppuStepCount > 0) {
_ppuStepCount--;
if(_ppuStepCount == 0) {
_cpuStepCount = 0;
SleepUntilResume();
}
}
}
void Debugger::SleepUntilResume()
{
_console->GetSoundMixer()->StopAudio();
_disassembler->Disassemble();
_console->GetNotificationManager()->SendNotification(ConsoleNotificationType::CodeBreak);
_executionStopped = true;
while(_cpuStepCount == 0 || _breakRequestCount) {
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(10));
}
_executionStopped = false;
}
void Debugger::ProcessStepConditions(uint8_t opCode, uint32_t currentPc)
{
if(_breakAddress == currentPc && (opCode == 0x60 || opCode == 0x40 || opCode == 0x6B || opCode == 0x44 || opCode == 0x54)) {
//RTS/RTL/RTI found, if we're on the expected return address, break immediately (for step over/step out)
_cpuStepCount = 0;
}
2019-03-03 16:34:23 -05:00
}
2019-03-01 20:27:49 -05:00
void Debugger::ProcessBreakConditions(MemoryOperationInfo &operation, AddressInfo &addressInfo)
{
if(_breakpointManager->CheckBreakpoint(operation, addressInfo)) {
_cpuStepCount = 0;
}
2019-03-07 20:12:32 -05:00
if(_cpuStepCount == 0 || _breakRequestCount) {
SleepUntilResume();
2019-03-07 20:12:32 -05:00
}
}
void Debugger::ProcessInterrupt(uint32_t originalPc, uint32_t currentPc, bool forNmi)
2019-03-24 12:05:51 -04:00
{
_callstackManager->Push(_prevProgramCounter, currentPc, originalPc, forNmi ? StackFrameFlags::Nmi : StackFrameFlags::Irq);
2019-03-24 12:05:51 -04:00
_eventManager->AddEvent(forNmi ? DebugEventType::Nmi : DebugEventType::Irq);
}
2019-03-07 20:12:32 -05:00
void Debugger::ProcessEvent(EventType type)
{
switch(type) {
2019-03-07 20:28:48 -05:00
case EventType::StartFrame:
_console->GetNotificationManager()->SendNotification(ConsoleNotificationType::EventViewerRefresh);
_eventManager->ClearFrameEvents();
break;
2019-03-01 20:27:49 -05:00
}
}
2019-02-27 20:33:56 -05:00
int32_t Debugger::EvaluateExpression(string expression, EvalResultType &resultType, bool useCache)
{
2019-03-01 20:27:49 -05:00
MemoryOperationInfo operationInfo { 0, 0, MemoryOperationType::Read };
2019-02-27 20:33:56 -05:00
DebugState state;
2019-03-01 20:27:49 -05:00
GetState(state);
2019-02-27 20:33:56 -05:00
if(useCache) {
return _watchExpEval->Evaluate(expression, state, resultType, operationInfo);
} else {
ExpressionEvaluator expEval(this);
return expEval.Evaluate(expression, state, resultType, operationInfo);
}
}
void Debugger::Run()
{
_cpuStepCount = -1;
_breakAddress = -1;
_ppuStepCount = -1;
}
void Debugger::Step(int32_t stepCount, StepType type)
{
switch(type) {
case StepType::CpuStep:
_cpuStepCount = stepCount;
_breakAddress = -1;
_ppuStepCount = -1;
break;
case StepType::CpuStepOut:
_breakAddress = _callstackManager->GetReturnAddress();
_cpuStepCount = -1;
_ppuStepCount = -1;
break;
case StepType::CpuStepOver:
if(_prevOpCode == 0x20 || _prevOpCode == 0x22 || _prevOpCode == 0xFC || _prevOpCode == 0x00 || _prevOpCode == 0x02 || _prevOpCode == 0x44 || _prevOpCode == 0x54) {
//JSR, JSL, BRK, COP, MVP, MVN
_breakAddress = (_prevProgramCounter & 0xFF0000) | (((_prevProgramCounter & 0xFFFF) + DisassemblyInfo::GetOperandSize(_prevOpCode, 0) + 1) & 0xFFFF);
_cpuStepCount = -1;
_ppuStepCount = -1;
} else {
//For any other instruction, step over is the same as step into
_cpuStepCount = 1;
_breakAddress = -1;
_ppuStepCount = -1;
}
break;
case StepType::PpuStep:
_ppuStepCount = stepCount;
_cpuStepCount = -1;
_breakAddress = -1;
break;
}
}
bool Debugger::IsExecutionStopped()
{
2019-03-07 20:12:32 -05:00
return _executionStopped;
}
void Debugger::BreakRequest(bool release)
{
if(release) {
_breakRequestCount--;
} else {
_breakRequestCount++;
}
}
2019-03-01 20:27:49 -05:00
void Debugger::GetState(DebugState &state)
{
2019-03-01 20:27:49 -05:00
state.Cpu = _cpu->GetState();
state.Ppu = _ppu->GetState();
}
shared_ptr<TraceLogger> Debugger::GetTraceLogger()
{
return _traceLogger;
2019-02-15 21:33:13 -05:00
}
shared_ptr<MemoryDumper> Debugger::GetMemoryDumper()
{
return _memoryDumper;
}
shared_ptr<Disassembler> Debugger::GetDisassembler()
{
return _disassembler;
}
2019-03-01 20:27:49 -05:00
shared_ptr<BreakpointManager> Debugger::GetBreakpointManager()
{
return _breakpointManager;
}
2019-03-03 16:34:23 -05:00
shared_ptr<PpuTools> Debugger::GetPpuTools()
{
return _ppuTools;
}
2019-03-07 20:12:32 -05:00
shared_ptr<EventManager> Debugger::GetEventManager()
{
return _eventManager;
}
2019-03-24 12:05:51 -04:00
shared_ptr<CallstackManager> Debugger::GetCallstackManager()
{
return _callstackManager;
}
2019-03-07 20:12:32 -05:00
shared_ptr<Console> Debugger::GetConsole()
{
return _console;
}